caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* class subtyping
@ 2004-12-17 15:00 nakata keiko
  2004-12-17 16:17 ` Martin Sandin
  0 siblings, 1 reply; 3+ messages in thread
From: nakata keiko @ 2004-12-17 15:00 UTC (permalink / raw)
  To: caml-list; +Cc: keiko

I have a question about subtyping between classes.

Why an instance of class a can not coerced into type b?

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) 

I got an error for the last statement.

I am reading the work on extension problem found at 
  http://pauillac.inria.fr/~remy/work/expr/
then, 
I am curious to know how ocaml compiler judges subtyping relation 
involving self types.

Regards,
Keiko NAKATA.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: class subtyping
  2004-12-17 15:00 class subtyping nakata keiko
@ 2004-12-17 16:17 ` Martin Sandin
  0 siblings, 0 replies; 3+ messages in thread
From: Martin Sandin @ 2004-12-17 16:17 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: class subtyping
@ 2004-12-17 16:18 Martin Sandin
  0 siblings, 0 replies; 3+ messages in thread
From: Martin Sandin @ 2004-12-17 16:18 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2004-12-17 16:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-17 15:00 class subtyping nakata keiko
2004-12-17 16:17 ` Martin Sandin
2004-12-17 16:18 Martin Sandin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).