« Return to Thread: Real (trivial) mixins

Re: Real (trivial) mixins

by Jeremy Tregunna-2 :: Rate this Message:

Reply to Author | View in Thread


I suppose a use case would be apropos. It'll be very bad, but design  
isn't what this is about.

Consider the utility object "Math" containing an "add" method which,  
ironically enough, adds two numbers together.

Math := Object clone do(
     add := method(n, m, n + m)
)

Consider another object Foo, which mixes in this object:

Foo := Object clone do(
     Math asMixin
)

Now, we can run:

Foo add(2, 3) // yields 5

Then, if we replace Math's add method, with this:

Math add := method(n, m, n * m)

And run Foo add(2, 3) again, we'll notice it still returns 5, while  
Math add(2, 3) returns 6 (since it's now doing multiplication instead  
of addition).

On 15-Apr-09, at 12:40 PM, Jeremy Tregunna wrote:

> Just thought I'd put this out there, topic came up in #io today about
> real mixins (not utilizing differential inheritance), so I set out to
> write them and realized that, it's extremely trivial. 5 lines of well
> structured code, or one line of still readable code. I'll put it in
> the former just because. :) It's presented below:
>
> Object asMixin := method(
>     self slotNames foreach(name,
>         call sender setSlot(name, self getSlot(name))
>     )
> )
>
> Enjoy.
>
> Regards,
>
> Jeremy Tregunna
> jeremy.tregunna@...
>
>
>
>
>
> ------------------------------------
>
> Yahoo! Groups Links
>
>
>

Regards,

Jeremy Tregunna
jeremy.tregunna@...



 « Return to Thread: Real (trivial) mixins