Hacker News new | past | comments | ask | show | jobs | submit login

Funny you mention that. If you had your own implementation of variables, you could probably override the behaviour of parameters in the #returning method to get the behavior in the OP without rewriting code.

Naturally the syntax looks a little ugly to the ALGOL-shaped eye, but imagine such a thing written into the language. It would be trivial to permit the ALOGOL-like syntax in the code but also allow you to override the #get and #set methods when you want to.

IIRC, this is exactly what Generalized Variables are in Common Lisp. Variables where you override the meaning of setting and getting their value.

Ruby sorta kinda allows this in a very limited case. While you cannot override the meaning of:

    foo = "foo"
You can create setter method such as:

    class MyStack
      
      # ...

      def top
        @array[-1]
      end

      def top=(value)
        @array[-1] = value
      end

    end
Thus, stack.top and stack.top = "foo" can do whatever you want.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: