On the current 1.7.1 snapshot (
because http://groovyconsole.appspot.com/ is still on 1.7-rc-1), you can also do the following - provide a map based constructor for the inner class and then you can use the named parameters without explicitly passing "this" as in
http://groovyconsole.appspot.com/script/68001:
----------------------------------------------
class Dog {
def color, bark
class Bark {
def pitch, volume
Bark(props) {
pitch = props.pitch
volume = props.volume
}
}
Dog(c) {
color = c
bark = new Bark(pitch: "low", volume: "loud")
}
String toString() {
return "The $color dog barks ${bark.pitch} and ${bark.volume}"
}
}
println new Dog("yellow")
----------------------------------------------
On Tue, Feb 9, 2010 at 10:15 AM, John Bito
<jwbito@gmail.com> wrote:
A static inner class doesn't have access to the containing class' this, so it's not surprising that it doesn't have that problem. I'm not really familiar with how one is supposed to use inner classes in Groovy - the compiler seems not to be doing the right thing.
One can call the inner class constructor if the containing instance (this) is passed explicitly: http://groovyconsole.appspot.com/script/68001
The Java compiler supplies this implicitly.
On Sun, Jan 31, 2010 at 19:14, Roger Studner
<rstudner@gmail.com> wrote:
so I wrote this email just a bit ago:
I have a class, with this inner class:
public class PhoneNumber {
public String fine
public String fullNumber
}
if I do:
def someMethod() {
def r = new PhoneNumber(fine:"hi", fullNumber:"dog")
}
I get:
Exception in thread "main" groovy.lang.GroovyRuntimeException: failed to invoke constructor: public comp.Outerclass$PhoneNumber(comp.Outerclass) with arguments: [] reason: java.lang.IllegalArgumentException: wrong number of arguments
Umm.. that is the right # of arguments.
Everything is fine if the inner class is static.
but like.. why? I mean, i use/instantiate inner classes all the time in java.. from the parent class, without them being static
Thanks,
Roger
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email