Xref: utzoo comp.lang.c++:4245 comp.lang.eiffel:342 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cornell!uw-beaver!teknowledge-vaxc!unix!hplabs!hplabsz!cook From: cook@hplabsz.HPL.HP.COM (William Cook) Newsgroups: comp.lang.c++,comp.lang.eiffel Subject: Re: Implementing virtual functions that return reference to self Summary: Eiffel typing of methods that return self Keywords: declaration by association, self, static typing Message-ID: <3685@hplabsz.HPL.HP.COM> Date: 2 Aug 89 17:51:52 GMT References: <8975@thorin.cs.unc.edu> <7131@microsoft.UUCP> <7137@microsoft.UUCP> <9694@alice.UUCP> Organization: Hewlett-Packard Laboratories Lines: 60 Recently there has been some discussion on problems in typing methods that return "self". I want to point out that Eiffel allows these methods and gives them their most natural typing. Unfortunately, Eiffel does not seem to be perfect in this respect, in that it allows some typings that are insecure (see my paper, "A Proposal for Making Eiffel Type-safe", in the proceedings of ECOOP'89). Eiffel uses "Current" instead of "self" or "this". It also has a novel typing construct called _declaration by association_ which allows a type of the form "like Current" to represent the type of a method that returns "Current". The example given by Michael T. Kelley, which was illegal in C++, can be coded in Eiffel. I haven't run this code, so it may not be perfect. The keyword "is" indicates the return value type of a method. class Base feature hello is Like Current do output("hello"); Result := Current; end end class Derived inherit Base feature hello is Like Current do output("g'day "); Result := Current; end goodbye is do output("see ya!"); end end class Main feature Create is local d : Derived; do d.hello.goodbye; -- type-correct end; end A general "copy" method can also be implemented with the same typing, by "creating" an object of type "Like Current". I don't understand Andrew Koenig's conclusion that this behavior should be illegal because you don't know the type at compile-time. I thought not knowing the precise type of things at compile-time is essential to OOP. When working with a variable v:Base one can only know that v.hello will be an instance of some subclass of Base. But this is enough because any such object will handle all the messages defined in Base. -william cook@hplabs.hp.com