Version française
Home     About     Download     Resources     Contact us    

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

Browse thread
[Caml-list] polymorphic methods in objects?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Nicolas Cannasse <warplayer@f...>
Subject: Re: [Caml-list] polymorphic methods in objects?
> Why can't objects have polymorphic methods ? I'm trying a compile and
> ocamlc gets upset because I have a method with an unbound type in its
> signature (see below for ocamlc error output).
> Must methods be type paramaterized through some kind of a template
> mechanism? This seems a bit limiting: the methods of an object would have
> their type signature fixed across the lifetime of an object.
>       method dummy_init : 'a -> unit
>       method dump : out_channel -> unit
>       method init : unit -> unit
>       method join : string -> unit
>       method null : unit -> unit
>       method resolv : string -> hmap option
>     end
> The method dummy_init has type 'a -> unit where 'a is unbound

two ways of doing that :

first, having a parameterized object :

class ['a] my_object : object
    method dummy_init : 'a -> unit

But perhaps here you really want that any value passed to your object is ok.
So you have to use a polymorphic method, which is not bound to the class
type :

class my_object : object
    method dummy_init : 'a . 'a -> unit

This way, you cannot of course store the parameter into a field of my_object
this it is not polymorphic.

Nicolas Cannasse

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: