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
Type problem... possible design problem
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-12-18 (04:00)
From: Matt Gushee <mgushee@h...>
Subject: Re: [Caml-list] Type problem... possible design problem
chris.danx wrote:

> type childNode = LeafNode of sgLeaf | IntNode of sgInternal
> and
> class sgLeaf
   .... etc. ...

> I've not programmed in O'Caml for a while and am not yet fully 
> comfortable with ocamls object system anyway, so I may be being silly. 
> Can anyone shed some light on a solution to this?

This seems to me like a good case for class types. How about something like:

class type leafType =
     method draw : unit -> unit

class type ['a] internalType =
     method addChild : 'a -> unit
     method getChildren : unit -> 'a list
     method draw : unit -> unit

type childNode =
     LeafNode of leafType
   | IntNode of childNode internalType

Then, of course, you'll need to define concrete classes that fit the 
class type signatures and/or coerce objects to those types.

I'm not sure this is the most elegant solution, but it is at least 
syntactically valid, and seems to do what you want.

Matt Gushee
Englewood, CO, USA