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
[Caml-list] polymorphic methods
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-03-13 (01:57)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] polymorphic methods
From: Damien <Damien.Pous@ens-lyon.fr>

> Does someone know why the following class type
> is not accepted ?
> # class type a = object method m: 'a. (#a as 'a) -> unit end;;
> The abbreviation #a expands to type #a but is used with type < .. >

Essentially because the type abbreviation #a is not yet fully defined
when its methods are typed. So what is happening here is that the type
uses a first definition of #a, with no methods at all, and then tries
to unify it with the real #a, with a method m, and fails because the
first one is supposed to be polymorphic.
I admit the error message could be clearer, but polymorphic methods
are a recent addition to ocaml, and it is really hard to give proper
error messages for their definitions.

Now, the serious part: can such a type be defined?
After working a bit on this riddle, I'm afraid the answer is no.
The reason is that there is no way to recurse on a quantified
polymorphic method rather than on the whole object.
The closest I found is:
  class type a =
    object method m : < d : 'b. (< m : 'a; .. > as 'b) -> unit > as 'a end
The dummy wrapper <d : 'b. ... > is just there to allow one to recurse
on the polymoprhic method itself. You can call the method m by doing
[a#m#d a'] in place of [a#m a'].

Thank you for pointing at this interesting phenomenon.

Jacques Garrigue

To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners