* Recursive class+type definitions
@ 2007-07-16 1:29 Raj B
2007-07-16 2:00 ` [Caml-list] " Jon Harrop
2007-07-16 2:27 ` Chris King
0 siblings, 2 replies; 4+ messages in thread
From: Raj B @ 2007-07-16 1:29 UTC (permalink / raw)
To: caml-list
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?
Thanks
Raj
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Recursive class+type definitions
2007-07-16 1:29 Recursive class+type definitions Raj B
@ 2007-07-16 2:00 ` Jon Harrop
2007-07-16 2:27 ` Chris King
1 sibling, 0 replies; 4+ messages in thread
From: Jon Harrop @ 2007-07-16 2:00 UTC (permalink / raw)
To: caml-list
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Recursive class+type definitions
2007-07-16 1:29 Recursive class+type definitions Raj B
2007-07-16 2:00 ` [Caml-list] " Jon Harrop
@ 2007-07-16 2:27 ` Chris King
2007-07-16 9:00 ` Jacques GARRIGUE
1 sibling, 1 reply; 4+ messages in thread
From: Chris King @ 2007-07-16 2:27 UTC (permalink / raw)
To: Raj B; +Cc: caml-list
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Recursive class+type definitions
2007-07-16 2:27 ` Chris King
@ 2007-07-16 9:00 ` Jacques GARRIGUE
0 siblings, 0 replies; 4+ messages in thread
From: Jacques GARRIGUE @ 2007-07-16 9:00 UTC (permalink / raw)
To: colanderman; +Cc: rajb, caml-list
From: "Chris King" <colanderman@gmail.com>
> 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).
I would think that one recursive module is sufficient.
Note also that, as this virtual class has no concrete methods, it
could just be defined as a class type (just means you can only use it
in interfaces and type annotations.) Then there is no code
duplication.
module rec Mypoint : sig
class type p =
object
method getx : float
method getlist : Mypoint.plist
end
type plist = Empty | Cons of (p * Mypoint.plist)
end = Mypoint
Jacques Garrigue
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2007-07-16 9:01 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-07-16 1:29 Recursive class+type definitions Raj B
2007-07-16 2:00 ` [Caml-list] " Jon Harrop
2007-07-16 2:27 ` Chris King
2007-07-16 9:00 ` Jacques GARRIGUE
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox