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: Chris King <colanderman@g...>
Subject: Re: [Caml-list] Recursive class+type definitions
On 7/15/07, Raj B <rajb@rice.edu> wrote:
> 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?

I'd write something like:

type mypointlist = Empty | Cons of (< getx: float; getlist:
mypointlist > * mypointlist)

class virtual ...


or:

type mypointlist = Empty | Cons of (mypoint * mypointlist)
and mypoint = < getx: float; getlist: mypointlist >

in the case that having an inheritable virtual class isn't important to you.

You can get the exact behavior you want using a pair of recursive
modules (see the "Language Extensions" section of the manual), one for
each of mypoint and mypointlist, but it's a lot more verbose (since
recursive modules require you to explicity give their signatures).

HTH,
Chris