Java Mailing List Archive

http://www.gg3721.com/

Home » user.groovy »

Re: [groovy-user] follow up

Guillaume Laforge

2010-02-09

Replies: Find Java Web Hosting

Author LoginPost Reply
Oh I thought I had upgraded to 1.7.0 on the console :-O
I guess I'll have to do the upgrade rapidly!

On Tue, Feb 9, 2010 at 16:40, Roshan Dawrani <roshandawrani@(protected):
> 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@(protected):
>>
>> 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@(protected):
>>>
>>> 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
>>>
>>>
>>
>
>



--
Guillaume Laforge
Groovy Project Manager
Head of Groovy Development at SpringSource
http://www.springsource.com/g2one

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email


©2008 gg3721.com - Jax Systems, LLC, U.S.A.