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: -- (:)
From: chris.danx <chris.danx@n...>
Subject: Type problem... possible design problem

What is the solution to the following problem?  There are two types of 
object in a scene graph, those that may have children and those may not. 
     A leaf may not have children.

For various reasons, I need to preserve the methods of internal nodes 
(non leaves).  I thought about something like this

type childNode = LeafNode of sgLeaf | IntNode of sgInternal
class sgLeaf
   = object(self)

     method draw () = ()

class sgInternal
   = object(self)

     method addChild (child : childNode) = ()

     method getChildren () = ...

     method draw () = ()

but this isn't legal O'Caml.  The following also doesn't work.

type childNode =
     LeafNode of <draw:unit -> unit; ..>
   | IntNode  of <draw:unit -> unit; addChild: childNode -> unit; ..>

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?

Basically I need to be able to call addChild, etc for internal nodes if 
they're not leaves so I need to remember whether the type contains only 
draw or it also contains addChild, etc.  It will be clients that call 
operations like addChild, getChildren, ...

Perhaps this is the wrong solution entirely.