English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

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 (00:42)
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.