This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

[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: -- (:) From: Remi VANICAT Subject: Re: [Caml-list] Polymorphic methods
```Frederic Tronel <Frederic.Tronel@inrialpes.fr> writes:

> Hello list,
>
>
> I've just compiled the new 3.05 version (I will change for 3.06
> tomorrow),
> and start playing a little bit with polymorphic methods.
> But this small piece of code does not compile:
>
> class node =
> object
> val mutable t = None
> method set_t: 'a. 'a -> unit = fun nt -> t <- Some nt
> end
>
> File "essai.ml", line 4, characters 31-53:
> This method has type 'a -> unit which is less general than 'b. 'b ->
> unit

you should look to this bug :
http://caml.inria.fr/bin/caml-bugs/fixed?id=1274;page=44;user=guest

To be more precise, the type of t can't be polymorphic, so the type of
set_t can't too.

look to this example :

class node =
object
val mutable t = None
method set_t: 'a. 'a -> unit = fun nt -> t <- Some nt
method get_t: 'a. unit -> 'a option = fun () -> t
end

the one could do this :

let t : int =
let a = new node
a #set_t 1.23
match a #get_t () with Some x -> x

which is obviously wrong.

you probably want :

class ['a] node =
object
val mutable t = None
method set_t: 'a -> unit = fun nt -> t <- Some nt
method get_t: unit -> 'a option = fun () -> t
end

--
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat
-------------------
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

```