jastrachan | 2 Apr 10:59 2004

Re: Re: Syntax sugar for Java Beans property declaration (was Re: [groovy-user] Mike on Properties)

On 2 Apr 2004, at 08:39, Jon Tirsen wrote:

> jastrachan@... wrote:
>> On 1 Apr 2004, at 16:20, Brian McCallister wrote:
>>> I don't think ruby uses macros (in the sense of generating code) per 
>>> se, it just calls a method which happens to add getters and setters 
>>> on every new instance of that type...
>> Thats pretty much a macro isn't it? - a piece of code that executes 
>> during the compilation of a class to modify the AST. At least thats 
>> how I understand it, please correct me if I'm wrong.
> It's not modifying the AST, it's executed at "compile-time" (well, 
> there is not really any compile-time in Ruby, more like 
> class-definition-time) and probably using eval to define methods that 
> reads and writes fields. Actually defining "attr_reader" should be 
> something like:
> def attr_reader name
>    eval %{
>       def #{name}
>           <at> #{name}
>       end
>    }
> end
> In case you don't know Ruby it's used like this:
> class Foo
>    attr_reader :bar
> end
> So it's not modifying the AST, it's not using some special macro 
> language. It's just using the fact that Ruby has such a clean and 
> simple object and execution model. This is why you love Ruby so much.

Well its code running at 'compile time' to change the class definition. 
If we were to do something similar in Groovy we could run some code 
while we were compiling the class to create properties - which is 
basically a compile time macro - which could programatically add new 
properties/fields/methods. But then we're in the realm of compile-time