You can't blame Scala for being flexible here!
Seriously, if it wasn't so practical in so many scenarios, do you honestly
think that people would be praising it quite as much as we do?
Anyway, case classes with named+default parameters DO allow the builder
pattern. Mostly by making it obsolete through the copy method:
case class Node(x : Int = 0, y : Int = 0, z : Int = 0)
val n = new Node
val n1 = n copy (x = 1)
val n2 = n1 copy (y = 2)
val n3 = n2 copy (z = 3)
or daisy-chain the calls:
val n4 = (new Node) copy (x = 1) copy (y = 2) copy (z = 3)
It's not the way I'd recommend it though, you're better off using the named
params directly:
val n5 = Node(
x = 1,
y = 2,
z = 3
)
And do feel free to leave out any params you're not concerned about, they
all have defaults:
val n6 = Node(
x = 1,
z = 3
)
val n7 = Node(
y = 2
)
At this point, users of JavaFX script should be starting to feel an eerie
sense of familiarity...
On 12 October 2010 20:59, Casper Bang <[email protected]> wrote:
> Wow, there's Scala again. Amazing how it somehow always sneaks in. As
> to the topic at hand, I'd suggest submitting a request for
> enhancement. The right people (Tulach etc.) can probably have this
> done in a jiff with a check-box in the existing accessor wizard.
>
> On Oct 12, 9:46 pm, Ricky Clarkson <[email protected]> wrote:
> > Scala's case classes plus named arguments really solve this well,
> > particularly if the calling code can be Scala too.
> >
> > case class Node(x: Float, y: Float)
> >
> > On Tue, Oct 12, 2010 at 8:42 PM, B Smith-Mannschott
> >
> >
> >
> >
> >
> >
> >
> > <[email protected]> wrote:
> > > OK, so we've established that neither Eclipse nor Lombok will do what
> > > the OP needs. Are there any other alternatives?
> >
> > > I found myself needing something very similar only last week. In my
> > > case, it was for a simple immutable value type (using Lombok's
> > > lovely @Data) to which I wanted to add fluent builder style methods,
> > > e.g.:
> >
> > > @Data
> > > class Node {
> > > private final float x;
> > > private final float y;
> > > // Lombok generates getX(), getY(), but not setX(), setY()
> > > // because x, y are final.
> > > // Lombok generates Node(float x, float y) constructor
> > > // since x and y are final.
> > > Node withX(float x) {
> > > return new Node(x, y);
> > > }
> > > Node withY(float y) {
> > > return new Node(x, y);
> > > }
> > > }
> >
> > > I ended up firing up emacs and defining a ad-hoc keyboard macro
> > > to grind out the code for me. Yea! More boilerplate for the next
> > > developer to wade through! huzzah!
> >
> > > How difficult would it be for someone inexperienced with Lombok's
> > > internals to add something like this?
> >
> > > Stuff like this is why I'm glad there's more than just Java on the
> > > JVM. For example, a Lisp (like Clojure) makes this kind of code
> > > generation drudgery easy via its civilized [1] macro support.
> >
> > > [1] where uncivilized == the C preprocessor.
> >
> > > // Ben
> >
> > > On Tue, Oct 12, 2010 at 18:27, Reinier Zwitserloot <[email protected]>
> wrote:
> > >> project lombok can do this without cluttering up your code:
> > >>http://projectlombok.org/(disclaimer: I'm a lombok developer). It
> > >> works in both eclipse and netbeans (and the command line).
> >
> > >> Eclipse has built in support to generate these (in the source menu,
> > >> "generate getters/setters"). I'm fairly sure netbeans has something
> > >> similar, no plugins required. They do actually stick text in your
> > >> source files that you then have to maintain, though, unlike Lombok.
> >
> > >> As far as I know none of these generate the 'return this' style
> > >> setter, because that style of setter does not adhere to the bean
> > >> standard.
> >
> > >> On Oct 12, 5:22 pm, Peter A Pilgrim <[email protected]> wrote:
> > >>> Hi Everyone
> >
> > >>> May be even Tor can help.
> >
> > >>> Has anyone come across a name value pattern plugin for NetBeans or
> > >>> Eclipse IDE?
> > >>> Given a class like this:
> >
> > >>> class Node {
> > >>> private float x;
> > >>> private float y;
> > >>> private float z;
> >
> > >>> }
> >
> > >>> The plugin generates the accessors and builder chain mutators
> >
> > >>> class Node {
> > >>> private float x;
> > >>> private float y;
> > >>> private float z;
> >
> > >>> public float getX() { return x; }
> > >>> public Node setX( float x ) { this.x = x; return this }
> > >>> public float getY() { return x; }
> > >>> public Node setY( float y ) { this.y = y; return this }
> > >>> public float getZ() { return z; }
> > >>> public Node setZ( float z ) { this.z = z; return this }
> >
> > >>> }
> >
> > >>> TIA
> >
> > >> --
> > >> You received this message because you are subscribed to the Google
> Groups "The Java Posse" group.
> > >> To post to this group, send email to [email protected].
> > >> To unsubscribe from this group, send email to
> [email protected]<javaposse%[email protected]>
> .
> > >> For more options, visit this group athttp://
> groups.google.com/group/javaposse?hl=en.
> >
> > > --
> > > You received this message because you are subscribed to the Google
> Groups "The Java Posse" group.
> > > To post to this group, send email to [email protected].
> > > To unsubscribe from this group, send email to
> [email protected]<javaposse%[email protected]>
> .
> > > For more options, visit this group athttp://
> groups.google.com/group/javaposse?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "The Java Posse" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<javaposse%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/javaposse?hl=en.
>
>
--
Kevin Wright
mail / gtalk / msn : [email protected]
pulse / skype: kev.lee.wright
twitter: @thecoda
--
You received this message because you are subscribed to the Google Groups "The
Java Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/javaposse?hl=en.