groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alexander Klein <i...@aklein.org>
Subject Re: Additional code to be called from TupleConstructor
Date Wed, 06 Jan 2016 22:44:57 GMT
Hi Schalk,

I like the idea, but as a small modification why not do something like:

@TupleConstructor( init={ myInitMethod() } )
class Example {
  String foo
  String bar

  private void myInitMethod() {
    // Do more init work in here
  }
}

This way you could put all the initialization stuff into the closure
only if you like to - and for small actions this might be more compact.

What do you think?

Sascha


Am 06.01.2016 um 22:55 schrieb Schalk Cronjé:
> The following has come up for me in a number of cases. I don't think
> there is any support for it today in Groovy (correct me if I'm wrong,
> please), but I think it could be a useful addition.
>
> I like to do some additional intialisation work once the properties
> have been populated,
>
>     class Example {
>       String foo
>       String bar
>
>       Example( Map props = [:] ) {
>         // Do some work here to initialise properties from the
>     supplied map
>
>         // Do some other initialisation work, possibly calling a
>     method in order to
>         // initialise private field 'something'
>       }
>
>       private def something
>     }
>
> I am a big fan of @TupleConstructor, but it will not work for the
> above case. Here, I am thinking that if one could do something like
>
>     @TupleConstructor( init=myInitMethod )
>     class Example {
>       String foo
>       String bar
>
>       private void myInitMethod() {
>         // Do more init work in here
>       }
>     }
>
> it would be quite useful. I don't know what the limitations are in
> order to implement somethin like this. It's a thought at present...
>
> -- 
> Schalk W. Cronjé
> Twitter / Ello / Toeter : @ysb33r


Mime
View raw message