usage help

41 views
Skip to first unread message

Thomas Ricaud

unread,
Jul 31, 2015, 5:27:12 AM7/31/15
to FreeBuilder
Hi,

I'm struggling with a specific use case.
Using the example your giving I'm trying to create a Person object and then modify it's age.

I did create the Person interface and the builder is indeed generated.

My issue is when i use the object. I can't find a way to update a value like age.

Person thomas = new Person.Builder().setAge(41).build();
thomas
.setAge(42);

The setAge function doesn't exist

I can not find a class i could extend to create such a function or any logic to implement.

Am i  missing anything ?

Thanks

Thomas Ricaud

unread,
Jul 31, 2015, 6:01:48 AM7/31/15
to FreeBuilder, thomas...@gmail.com
After putting my thinking cap on :) , the builder Pattern is best for immutable object. 

So i guess that's why i can't set my value: to prevent the usage of this annotation on a mutable object.

Chris Purcell

unread,
Jul 31, 2015, 2:34:09 PM7/31/15
to Thomas Ricaud, FreeBuilder
Hi Thomas!

That's exactly it: built objects are immutable. If you want to make a new Person with an updated age, you can do:

Person oldThomas = new Person.Builder()
        .mergeFrom(thomas)
        .setAge(42)
        .build();

Happy coding!
Chris

--
You received this message because you are subscribed to the Google Groups "FreeBuilder" group.
To unsubscribe from this group and stop receiving emails from it, send an email to freebuilder...@googlegroups.com.
To post to this group, send email to freeb...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/freebuilder/987bfcb5-c55a-44cb-be0a-bc4ab523adc6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages