Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!apple!oliveb!pyramid!leadsv!esl!dml From: dml@esl.UUCP (Denis Lynch) Newsgroups: comp.sys.next Subject: Re: Deferred methods and strong typing Message-ID: <932@esl.UUCP> Date: 6 Jul 89 21:30:45 GMT References: <130007@gore.com> Reply-To: dml@esl.UUCP (Denis Lynch) Organization: ESL, Inc., Sunnyvale, CA. Lines: 46 In article <130007@gore.com> jacob@gore.com (Jacob Gore) writes: >Consider a class that has a deferred method: > A.h: > #import > @interface A:Object {...} > - (A*)someMethod; >and a subclass that implements that method: > B.h: > #import "A.h" > @interface B:A {...} > - (B*)someMethod; >Now, consider the use of someMethod: > C.m: > #import "B.h" > [anInstanceOfB someMethod]; > >the compiler in >0.9 gives a "multiple declarations for method `someMethod'" warning, and >then uses A's someMethod. > >What's the scoop? The problem is philosophically thorny, but solving it is easy. Remember that Objective C insists on one system-wide template for each message selector. That is, if one class has a method (int)lifeTime then all lifeTime messages must return (int). Your example has introduced two definitions for someMethod: one says it will return (A*) and one says (B*). This violates Objective C's ground rules. Philosophically your example makes sense, since indeed all Bs are As, so B's definition of someMethod is consistent with A's, just more specific. It would seem that Objective C doesn't analyse this case as carefully as you did, so it missed the subtlety. You shouldn't have any trouble if you just change the defintion of B's someMethod to (A*). Returning a B will be legal (since Bs are As), and the Objective C method definition things won't get in the way. Good luck! -- Denis Lynch ESL Incorporated decwrl!borealis!\ ARPA: dml%esl.ESL.COM@ames.arc.nasa.gov ucbcad!ucbvax!- ames!- esl!dml SMAIL: dml@esl.ESL.COM lll-lcc!/