Re: How to write *include* like Ruby?
Jeremy Tregunna <[email protected]> Sat, 19 May 2012 15:33:41 -0600
| Newsgroups | gmane.comp.lang.io |
|---|---|
| Message-ID | <[email protected]> |
Traits by definition require explicit renaming of conflicting slots, that's how I've implemented them in Io (c.f. addTrait). I.e., try this:
Foo := Object clone do(foo := 42)
Bar := Object clone do(foo := 23)
Bar addTrait(Foo)
and you should get an exception. However, do this:
Bar addTrait(Foo, Map clone atPut("foo", "bar"))
and Foo's "foo" method will be set on Bar as "bar". I.e.,
Bar bar == 42
Regards,
Jeremy Tregunna
On Saturday, 19 May, 2012 at 3:13 PM, Steve Dekorte wrote:
>
>
> Hi lijie,
>
> lijie wrote:
> > I want to write Io code like Ruby meta programming. *Module#include* is an important method to extend classes.
>
> It depends on how you want lookups and updates to work with your traits.
>
> 1. Traits not overriding object slots and updates to occur on the object:
>
> Object appendNonOverridingTrait := method(aTrait,
> self addProto(aTrait)
> return self
> )
>
> 2. Traits overriding object slots and updates to occur on the object:
>
> Object insertOverridingTrait := method(aTrait,
> traitInstance := aTrait clone setProto(self proto)
> self setProto(traitInstance)
> return self
> )
>
> With delegation (via overriding the forward method) you could implement:
> 3. Traits not overriding object slots and updates to occur on the trait
> 4. Traits overriding object slots and updates to occur on the trait
>
> These methods would require maybe 10 lines of code each. I can post implementations if that is the behavior you need.
>
> Cheers,
> Steve
>
>