Created
April 4, 2012 12:46
-
-
Save purplefox/2300852 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
It's nice being able to set properties by passing them to the constructor in Groovy: | |
def foo = new Foo(bar: 23, wibble: "hello") | |
But if Foo is created via a factory method instead: | |
def foo = Foo.createFoo(bar: 23, wibble: "hello") | |
It doesn't work | |
where | |
class Foo { | |
.. properties | |
static Foo createFoo() { | |
return new Foo(); | |
} | |
} | |
How can I get this to work with factories? |
OK, I finally understand. You're right, you can't get automatic property binding once you're no longer using the default constructor. I've been told it may be worth a JIRA :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No, I don't mean that.
I know Groovy can automatically apply properties without me having to code any manual property application code if I call a no arg constructor:
I.e.
This allows me to construct the object like so:
def foo = new Foo(port:80, host: "localhost")
The problem is my Foo class already takes another arg in its constructor (which is not a property), i.e. Foo is really like this:
I was hoping that Groovy would still automatically apply params in this case, i.e. allow me to do:
def foo = new Foo("wibble", [port:80, host: "localhost"])
but that doesn't seem to work.
hence.. I am applying the params manually