Browse thread
Re: class subtyping
- Martin Sandin
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: | 2004-12-17 (16:22) |
From: | Martin Sandin <om_sandin@y...> |
Subject: | Re: class subtyping |
nakata keiko wrote: > Why an instance of class a can not coerced into type b? I'll assume you mean the other way around :-) > class a = object (self : 'a) > method f (x : 'a) = x#g > method g = 0 > end > > class b = object > inherit a > method h = 1 > end > > let x = ((new b) :> a) Afaik the self type refers to actual type of the object, eg in object of class b it refers to type b. Thus, the method f of class b requires an object of type b as a parameter, and type b implies having a method h. Objects of class a don't have this method, and so b#f can't accept all the arguments a#f can. Google for co/contra variance for discussions of these issues. The below class avoids the problem: class a = object method f (x : a) = x#g method g = 0 end /Martin __________________________________ Do you Yahoo!? Yahoo! Mail - Find what you need with new enhanced search. http://info.mail.yahoo.com/mail_250