Version française
Home     About     Download     Resources     Contact us    
Browse thread
polymorphic method.
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] polymorphic method.
There are already polymorphic methods in ocaml.
The syntax for your example would be:

class myobject = object
  method id : 'a. 'a -> 'a = fun x -> x
end

A "polymorphic" keyword might seem simpler, but it would be complex to
handle the case where a polymorphic method type contains also class
parameters:

class ['a] cell (x : 'a) = object
  method pair : 'b. 'b -> 'a * 'b = fun y -> (x,y)
end

More generally, you might end up with types more polymorphic than you
expected, and since differently instantiated polymorphic method types
are incompatible, this would be a problem.

Jacques Garrigue

From: Guillaume Yziquel <guillaume.yziquel@citycable.ch>
> When developing with objects in OCaml, I'm quite often faced with
> polymorphic methods.
> 
> Such as:
> 
> class myobject = object
>   method id x = x
> end
> 
> Sometimes you have many methods that you're tinkling with, and the
> compiler keeps saying to you that 'a is inbound in this class
> declaration.
> 
> I'm therefore wondering if it would be a good idea to have a keyword
> 'polymorphic', and one would write
> 
> class myobject = object
>   polymorphic method id x = x
> end
> 
> The polymorphic keyword would be a hint that the method is polymorphic
> and that there is no need to look at the class' type parameters.
> 
> -- 
>      Guillaume Yziquel
> http://yziquel.homelinux.org/