You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Gerald Wiltse <je...@gmail.com> on 2016/03/18 16:51:39 UTC

Expando as a Trait?

Expando is a pretty cool object. And if we extend it, we get it's really
nice "behavior". Unfortunately, as extending = inheritance, thus extending
expando precludes us from extending our true parent classes. This is why
implementing interfaces and traits is often a better choice than
inheriting, especially when one just wants to compose behaviors such as
those offered by expando.

Which leads me to ask the question: is it crazy to suggest that the body of
Expando would actually make a good Trait?   Has this been discussed before?

I'm novice so there could be obvious reasons not to do this which I'm not
aware of.


Gerald R. Wiltse
jerrywiltse@gmail.com

Re: Expando as a Trait?

Posted by Gerald Wiltse <je...@gmail.com>.
Yes indeed.   Just as Tim posted here, the basics of this functionality can
be manually.

http://stackoverflow.com/questions/7078855/groovy-dynamically-add-properties-to-groovy-classes-from-inside-class-methods

However, his comment points out the fact that groovy expando is a nicely
polished and mature implementation of a dynamic object.

https://github.com/apache/groovy/blob/master/src/main/groovy/util/Expando.java

It just seems undesirable to manually add this boilerplate code into every
object I create in the future (where i want dynamic), when traits are so
nice in groovy.


Gerald R. Wiltse
jerrywiltse@gmail.com


On Fri, Mar 18, 2016 at 12:03 PM, Cédric Champeau <cedric.champeau@gmail.com
> wrote:

> I think what you are suggesting is close to this example in the docs:
> http://groovy-lang.org/objectorientation.html#_dynamic_methods_in_a_trait
>
> Am I right?
>
> 2016-03-18 16:51 GMT+01:00 Gerald Wiltse <je...@gmail.com>:
>
>> Expando is a pretty cool object. And if we extend it, we get it's really
>> nice "behavior". Unfortunately, as extending = inheritance, thus extending
>> expando precludes us from extending our true parent classes. This is why
>> implementing interfaces and traits is often a better choice than
>> inheriting, especially when one just wants to compose behaviors such as
>> those offered by expando.
>>
>> Which leads me to ask the question: is it crazy to suggest that the body
>> of Expando would actually make a good Trait?   Has this been discussed
>> before?
>>
>> I'm novice so there could be obvious reasons not to do this which I'm not
>> aware of.
>>
>>
>> Gerald R. Wiltse
>> jerrywiltse@gmail.com
>>
>>
>

Re: Expando as a Trait?

Posted by Cédric Champeau <ce...@gmail.com>.
I think what you are suggesting is close to this example in the docs:
http://groovy-lang.org/objectorientation.html#_dynamic_methods_in_a_trait

Am I right?

2016-03-18 16:51 GMT+01:00 Gerald Wiltse <je...@gmail.com>:

> Expando is a pretty cool object. And if we extend it, we get it's really
> nice "behavior". Unfortunately, as extending = inheritance, thus extending
> expando precludes us from extending our true parent classes. This is why
> implementing interfaces and traits is often a better choice than
> inheriting, especially when one just wants to compose behaviors such as
> those offered by expando.
>
> Which leads me to ask the question: is it crazy to suggest that the body
> of Expando would actually make a good Trait?   Has this been discussed
> before?
>
> I'm novice so there could be obvious reasons not to do this which I'm not
> aware of.
>
>
> Gerald R. Wiltse
> jerrywiltse@gmail.com
>
>