Slava,
Let's say I have these classes, which represent an object with a position,
velocity, and color respectively. (Put aside philosophical design issues for
a moment; stuff like "is an airplane a color?")
TUPLE: pos pos ;
TUPLE: vel vel ;
TUPLE: col col ;
With multiple inheritance, I could make an airplane class:
TUPLE: airplane < { pos vel col } ;
And now methods defined on pos, vel, or col would work on airplanes.
But, we don't have MI.
You can do duck typing. I can do this:
TUPLE: airplane pos vel col ;
Now, any words which operate on pos, vel, or col, objects will work
on 'airplane' as long as they only refer to the slots by name. Methods defined
on classes pos, vel, or col will not work on airplanes.
Duck typing is tacky.
So what about this:
TUPLE: pos pos ;
TUPLE: vel vel ;
TUPLE: col col ;
MIXIN: pos-mixin
MIXIN: vel-mixin
MIXIN: col-mixin
INSTANCE: pos pos-mixin
INSTANCE: vel vel-mixin
INSTANCE: col col-mixin
Let's make a 'distance' method for 'pos-mixin' objects:
GENERIC: distance ( a b -- c )
METHOD: distance { pos-mixin pos-mixin } [ pos>> ] bi@ v- norm ;
Now I make my airplane:
TUPLE: airplane pos vel col ;
The 'distance' method won't work on it yet. But now I do:
INSTANCE: airplane pos-mixin
And suddenly airplane responds to 'pos-mixin' methods.
I'm guessing there are major performance impacts when you do this. On the
other hand, once the compiler honors type declarations in effects, it seems
alot of dispatch even in these cases could be resolved at compile time. For
example:
: foo ( a:airplane b:airplane -- c ) distance ;
Given that, the compiler can find the right method at compile time. Once it
has the right method it can go further and resolve slot references. I.e. once
you have this (the particular method):
[ pos>> ] bi@ v- norm
The 'pos>>' can be resolved because we're working with airplanes. Moreover, if
the 'pos' slot on airplanes is typed, perhaps some of the other operations
can benefit from this information.
There are some subtle issues here. For example, if you are simulating MI this
way, when slots are typed, you need to make sure you also type the inheriting
class slots the same way. Having a declarative way to set all this up would
be cool.
I wanted your thoughts on this "technique". :-)
Ed
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk