Version française
Home     About     Download     Resources     Contact us    
Browse thread
Recursive class+type definitions
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jon Harrop <jon@f...>
Subject: Re: [Caml-list] Recursive class+type definitions
On Monday 16 July 2007 02:29:47 Raj B wrote:
> Hi all
>
> Can we have mutually recursive definitions mixing types and classes?
> For example, I want to define
> a class called mypoint, which contains another data structure (say, a
> custom list) of mypoints.
>
> class virtual mypoint =
> 	object
> 		method virtual getx : float
> 		method virtual getlist : mypointlist
> 	end
>
> type mypointlist = Empty | Cons of (mypoint * mypointlist)
>
> What is the best way to achieve this kind of mutual recursion?

Good question. No idea. But you can do this:

class virtual ['a] mypoint = object
  method virtual getx : float
  method virtual getlist : 'a
end;;

type mypointlist = Empty | Cons of (mypointlist mypoint * mypointlist);;

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
The OCaml Journal
http://www.ffconsultancy.com/products/ocaml_journal/?e