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] About polymorphic methods and recursive classes
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-03-26 (07:43)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] About polymorphic methods and recursive classes
From: Sebastien Briais <>

> So, if I understand you well, it is not possible to write something like 
> that in ocaml:
> class virtual ['a,'b] list_visitor =
> object
>   method virtual caseNil : 'a
>   method virtual caseCons : 'b -> 'b list -> 'a
> end
> and virtual ['b] list =
> object
>   method virtual visit : 'a.(('a,'b) list_visitor -> 'a)
> end
> But what is the reason of this limitation ? 
> I do not see the reasons since for example, in Generic Java, such a 
> definition is correct.

My previous answer was a bit vague, but actually the problem is more
intrinsic to ocaml objects.
Contrary to Java, Ocaml object types are defined structurally, rather
than by name. For this reason, recursion has to be limited to cases
where recursive occurences of a type constructor have always the
same arguments. But in your example, 'b list in list_visitor expands
to < visit : 'a. ('a,'b) list_visitor -> 'a >, where list_visitor has
different arguments from its definition (the 'a here is an universal
variable, different from the normal 'a listed as parameter in the

As I showed, the only workaround is to untie the mutual recursion by
replacing 'b list by an extra parameter.

This is a bit similar to the reason you cannot define a map method:
class type ['a] list = object
  method map : 'b. ('a -> 'b) -> 'b list
but in that case you cannot untie the recursion: 'b in 'b list is
locally bound.

Jacques Garrigue

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