atom feed11 messages in org.codehaus.groovy.devRe: [groovy-dev] What about a @Mutabl...
FromSent OnAttachments
Paulo Gabriel PoiatiJul 10, 2010 4:07 pm.diff
Paul KingJul 11, 2010 3:36 pm 
Guillaume LaforgeJul 12, 2010 1:54 am 
Erik PragtJul 12, 2010 2:28 am 
Dierk KönigJul 12, 2010 2:31 am 
Guillaume LaforgeJul 12, 2010 2:33 am 
Martin C. MartinJul 12, 2010 6:35 am 
Paulo Gabriel PoiatiJul 12, 2010 8:20 am 
Hamlet D'ArcyJul 12, 2010 11:43 pm 
Alex TkachmanJul 12, 2010 11:49 pm 
Hamlet D'ArcyJul 14, 2010 1:46 am 
Subject:Re: [groovy-dev] What about a @Mutable ast transformation (with implementation)
From:Paul King (pau@asert.com.au)
Date:Jul 11, 2010 3:36:09 pm
List:org.codehaus.groovy.dev

Hi Paulo, looks cool!

I added a reference to your post to the relevant issue: http://jira.codehaus.org/browse/GROOVY-2879

Thanks for the patch.

On 11/07/2010 9:08 AM, Paulo Gabriel Poiati wrote:

Hello, I think it's a good idea to have an ast transformation that create the toString, equals, hashCode and positional args constructor (like @Immutable does). Also, because it's mutable it can have untyped properties and not all properties need to be initialized in the constructor.

I was searching for something like that, and I found this: http://archive.codehaus.org/lists/org.codehaus.groovy.user/msg/1aae@mail.gmail.com

Paulo King suggested the @Mutable ast.

I did an implementation, based in the current groovy trunk (rev 20439). Most of the work was stole from Paul King @Immutable implementation).

What differs from @Immutable: - It's mutable, implying that nor properties or class are final, and it provide setters methods. - Allow untyped properties. - Objects are passed by reference in the constructor, not wrapped or cloned. - Hashcode is not cached. - Constructor arguments are optional. - If a class is annotated as @Mutable and @Immutable an exception is throw. - Inheritance is possible

The patch is attached...

Example:

*@Mutable class Customer {* * String first, last* * int age* * Date since* * Collection favItems = ['Food']* * def object * *}* * * *def d = new Date()* *def anyObject = new Object()* *def c1 = new Customer(first:'Tom', last:'Jones', age:21, since:d, favItems:['Books', 'Games'], object: anyObject)* *def c2 = new Customer('Tom', 'Jones', 21, d, ['Books', 'Games'], anyObject) * *assert c1 == c2* * * * * *def c3 = new Customer(last: 'Jones', age: 21)* *def c4 = new Customer('Tom', 'Jones')* *assert null == c3.since* *assert 0 == c4.age* *assert c3.favItems == ['Food'] && c4.favItems == ['Food']*

I will appreciate any kind of feedback.

Tks !

[]'s Paulo Poiati

blog.paulopoiati.com <http://blog.paulopoiati.com>