English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
oo type question
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2008-03-06 (15:48)
From: Michael Wohlwend <micha-1@f...>
Subject: Re: [Caml-list] oo type question
Am Donnerstag, 6. März 2008 14:18:29 schrieb Peng Zang:
> 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.

yes, that's the easy way, but then I have to do casts all over the place, not 
very nice

> 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:
>   method add : 'a. <id:int; ..> as 'a -> unit =
>     fun o -> ids <- o#id :: ids

I know that version and of course it works. I only  didn't understand (and 
still do not) why it's not possible to just declare
method add (a: #someClass) ... which seems natural for me. And it works for 
normal functions that way.

If you have many classes (actually it's for adding widgets to group widgets) 
it seems just a bit too complicated and not a natural way to declare such a 
simple thing.