On 11/12/09, Roger Pack <rogerpack2005 / gmail.com> wrote:
>> If you get stuck or can't understand something, don't hesitate to ask
>> me.
>
> Sweet I want to use macros.  How about one to support ++

Hmm, I had thought that ruby's immutable numeric types meant this was
impossible, but maybe it is feasible to implement as a macro... (not
as a method).

The big problem is that I haven't (uh, yet) written the magic that
will allow users to define their own operators. (Or (re)define
existing operators as macros...)

But other than that, this ought to be possible... you'd need to use
different names until operator macros are defined, tho.
The preincrement version could be written like this:

macro preincr(n)
  :( ^n+=1 )
end

postincrement is somewhat more difficult. Here's a first pass:

macro postincr(n)
  :( (res=^n;^n+=1;res) )
end

(Warning: both increment macros are untested.)

but postincr is problematic. A call like:
  postincr( a().b )
would cause a().b to be evaluated twice, which isn't right. Solving
this, so that side effects in the arg to postincr are evaluated only
once, is more complicated, and I won't attempt it here.

And nobody wants to type preincr or postincr (or even incr) when they
could just use +=1, so we'd really need user-defined operators
here....


> ? :)

Is that a self-portrait? ;)