Version française
Home     About     Download     Resources     Contact us    
Browse thread
Problem with recursive class and non-class types
[ 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] Problem with recursive class and non-class types
From: Goswin von Brederlow <goswin-v-b@web.de>
> I want to define the two types below:
> 
> type foo = { bar : bar; }
> class bar = object val mutable foo : foo list = [] end
> 
> Is there another way of doing this other than:
> 
> # type 'a foo = { bar : 'a; } 
>   class bar = object val mutable foo : #bar foo list = [] end;;
> type 'a foo = { bar : 'a; }
> class bar : object val mutable foo : #bar foo list end

The alternative is to use a recursive module, but this is actually
more verbose.

module rec M : sig
  type foo = { bar : M.bar; }
  class bar : object val mutable foo : foo list end
end = struct
  type foo = { bar : M.bar; }
  class bar =  object val mutable foo : foo list = [] end
end

You can avoid a bit of the verboseness by splitting types and values,
since recursive modules built only from types require no duplication.

module rec M : sig
  type foo = { bar : M.bar; }
  class type bar = object val mutable foo : foo list end
end = M

class bar : M.bar = object val mutable foo : M.foo list = [] end

You still need to provide an explicit interface for bar.

Hope this helps,

     Jacques Garrigue