Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] polymorphic method question
[ 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@k...>
Subject: Re: [Caml-list] polymorphic method question
From: nadji@noos.fr
> I am puzzled by this behaviour, can a guru explain me ?
> I have two classes
[...]
> I want to do a class gentoto :
> class gentoto = object
>   method gen : 'a . 'a -> 'a basic =
>     fun x -> (new toto x :> 'a basic )
> end;;
> But the compiler complains that :
>       Characters 64-98:
>       fun x -> (new toto x :> 'a basic )
>       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> This method has type 'a -> 'a basic which is less general than
>   'b. 'b -> 'b basic
> 
> ... but if I do :
> let newtotoasbasic x = (new toto x :> 'a basic );;
[...]
> The compiler agrees with me ...
> What is wrong with the first declaration ?

There is a problem with the scoping of named type variables in the
language, which makes them incompatible with polymorphic methods.
You can solve this by using anonymous variables:
# class gentoto = object
    method gen : 'a . 'a -> 'a basic =
      fun x -> (new toto x :> _ basic)
  end;;
class gentoto : object method gen : 'a -> 'a basic end

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