-
-
Save purplefox/2300852 to your computer and use it in GitHub Desktop.
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? |
someOtherArg does not represent a property so putting it in the map of properties does not make sense to me. I also tried it and it didn't seem to work
You can see how I fixed it here https://github.com/purplefox/vert.x/blob/no-singleton/src/main/groovy/org/vertx/groovy/core/Vertx.groovy
By applying the properties manually
Ooops, sorry, I copied the wrong snippet. Whether someOtherArg is treated as a property or not is irrelevant. The key point is that the first argument should be a map:
static Foo createFoo(Map properties, someOtherArg) {
return new Foo(someOtherArg, properties);
}
Foo.createFoo(jVertx, host: "google.com", port: 80)
Note that named arguments can appear anywhere in the argument list: they are all collated into a single map which becomes the first argument.
BTW, indent code blocks by four spaces and you'll get the code formatting in gists and their comments (it's standard Markdown).
I've tried the params both ways around and neither works for me.
Doing it manually does work though
https://github.com/purplefox/vert.x/blob/no-singleton/src/main/groovy/org/vertx/groovy/core/Vertx.groovy
https://github.com/purplefox/vert.x/blob/no-singleton/src/main/groovy/org/vertx/groovy/core/http/HttpClient.groovy
I can't help but think we're talking about different things here. What doesn't work for you exactly? Sorry, I'm just not understanding what you're getting at.
I want to Groovy to automatically apply the properties, as you can see from the link above I'm doing it manually.
If I understand you correctly, you want Groovy to recognise the factory pattern and automatically pass the factory method arguments through to the constructor of the created instance? Not sure any language does that :) You can always manage it via AST transforms, but that seems like wasted effort.
Anyway, what you have currently is what I would do, although I would probably make the Map the first argument of the constructors rather than the second.
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.
class Foo {
Foo() {
}
}
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:
class Foo {
Foo(someOtherParam) {
}
}
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
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 :)
I'm not sure what your solution is, but I thought I'd point out that if you specify the Map as the first argument, you can have proper named argument support:
I think that does what you want.