Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Peng Zang <peng.zang@gmail.com>
To: caml-list@yquem.inria.fr
Cc: Michael Wohlwend <micha-1@fantasymail.de>
Subject: Re: [Caml-list] oo type question
Date: Thu, 6 Mar 2008 08:18:29 -0500	[thread overview]
Message-ID: <200803060818.31857.peng.zang@gmail.com> (raw)
In-Reply-To: <200803061352.38360.micha-1@fantasymail.de>

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

The problem is that class ['a] store after adding o1 only accepts objects that 
have both an id method AND a hi method.  So to make it work you need to do 
something like this:


let o1 = object method id = 1  method hi = "hi" end;;
let o2 = object method id = 2  method ho = "ho" end;;
class ['a] store = object 
  val mutable ids = []
  method add (o: 'a) =  ids <- o#id :: ids
end;;
let s = new store ;;
s#add (o1 :> <id:int>);;
s#add (o2 :> <id:int>);;

Note how I coerce o1 and o2 both to a subtype before adding.  Now the problem 
goes away because to the s#add method o1 and o2 have the same type.



BUT, what I think you really want is a polymorphic method, not a polymorphic 
class.  The manual has a decent explanation under polymohrphic methods in the 
Objects section.  So I think you want this:


let o1 = object method id = 1  method hi = "hi" end;;
let o2 = object method id = 2  method ho = "ho" end;;
class store = object 
  val mutable ids = []
  method add : 'a. <id:int; ..> as 'a -> unit = 
    fun o -> ids <- o#id :: ids
end;;

let s = new store ;;
s#add o1;;
s#add o2;;



Peng



On Thursday 06 March 2008 07:52:38 am Michael Wohlwend wrote:
> I try to do the following:
>
> I have some objects (all with an id method):
> let o1 = object method id = 1  method hi = "hi" end
> let o2 = object method id = 2  method ho = "ho" end
>
> and now:
>
> class store = object
> 	val mutable  ids = []
> 	method add o =  ids <- o#id :: ids
> end
>
> this doesn't work, since the hidden type variable in the argument to add (I
> think). But why can't the compiler just set o to be of type < obj:int; ..>
> ?. So I try:
> class ['a] store = object
> 	val mutable  ids = []
> 	method add (o: 'a) =  ids <- o#id :: ids
> end;;
>
> ...but...
>
> let s = new store ;;
> give the type:
> val s : < id : '_a; _.. > store = <obj>
>
> so this doesn't work:
>
> s#add o1;
> s#add o2
>
> his expression has type < hi : string; id : int > but is here used with
> type < ha : string; id : int >
> The second object type has no method ho
>
> If I define a function, say:
> let f o = o#id;;
> I get :
> val f : < id : 'a; .. > -> 'a = <fun>
> so this works:
> f o1; f o2
>
>
> why does this not work with methods?
>
> cheers
>  Michael
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.7 (GNU/Linux)

iD8DBQFHz+8nfIRcEFL/JewRAm9iAJ9CsOmurw8tI2VUls4noUPN8L0TXgCeN6Zk
i0NwtHUdj2XN5lnZbCSbxtw=
=MHlA
-----END PGP SIGNATURE-----


  reply	other threads:[~2008-03-06 13:18 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-06 12:52 Michael Wohlwend
2008-03-06 13:18 ` Peng Zang [this message]
2008-03-06 15:45   ` [Caml-list] " Michael Wohlwend
2008-03-06 16:58     ` Peng Zang
2008-03-10  8:20     ` Jacques Garrigue
2008-03-10  8:38       ` Michael Wohlwend

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200803060818.31857.peng.zang@gmail.com \
    --to=peng.zang@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=micha-1@fantasymail.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox