Polymorphic catcalls
Regan Heath
regan at netmail.co.nz
Tue Apr 17 05:15:09 PDT 2012
A colleague of mine here at work is a fan of Eiffel and recently passed
this example of a problem with polymorphism which Bertrand Meyer called a
"Catcall" ('Cat' stands for 'Changed Availability or Type')
The original article by Meyer is here:
http://archive.eiffel.com/doc/manuals/technology/typing/cat.html
My colleague's example Eiffel code exhibiting the "problem" is here..
SKIER Eiffel class:
http://www.jurjevic.org.uk/programming/eiffel/examples/catcall/test/skier_chart.html
BOY Eiffel class:
http://www.jurjevic.org.uk/programming/eiffel/examples/catcall/test/boy_chart.html
GIRL Eiffel class:
http://www.jurjevic.org.uk/programming/eiffel/examples/catcall/test/girl_chart.html
This is how a boy can hope to share a room with a girl in Eiffel (at
present Eiffel compiler is incapable of detecting the error and exception
is thrown at run time):
http://www.jurjevic.org.uk/programming/eiffel/examples/catcall/test/example.html
His description of why it occurs is..
"That is because of the polymorphic assignment `s := b' and the fact that
in Eiffel it is allowed in a descendant class to covariantly change formal
argument types (in BOY class in 'share' call 'other' argument is
covariantly changed from SKIER to BOY, in GIRL class in 'share' call
'other' argument is covariantly changed from SKIER to GIRL)."
In D, we're not allowed to change the formal argument type of an interface
method when we implement it (I believe) so if we were to attempt to
implement something like this we'd be casting the "Skier other" to a "Boy
otherBoy" in the overridden method and then asserting/throwing on the
resulting null when a Girl was passed. So, we'd also solve the problem by
detecting it at runtime.
But it got me wondering, is it at all possible to solve the problem at
compile time.. I suspect not as by the time you assign an instance of a
Boy or Girl to a variable of type Skier I think you might lose the ability
to enforce anything WRT it's underlying/derived type at compile time. I
suspect the compiler would have to do some sort of code inspection to be
able to determine the derived type at compile time, and that's a bit much
to ask to solve something that's really just a consequence of polymorphism
itself IMO.
But then, I'm pretty terrible when it comes to using D's compile
time/generic/generative features so I thought I'd post this and see if
anyone could come up with anything.
Regan
--
Using Opera's revolutionary email client: http://www.opera.com/mail/
More information about the Digitalmars-d
mailing list