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
Objects, dynamic cast, Obj.magic abuse and dragons
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2008-02-26 (15:37)
From: Dirk Thierbach <dthierbach@g...>
Subject: Re: [Caml-list] Objects, dynamic cast, Obj.magic abuse and dragons
On Tue, Feb 26, 2008 at 12:35:10PM +0100, Berke Durak wrote:
> In this adventure, things can contain other things; a class physical has a 
> list of things it contains, and an optional pointer to its container.
> Persons, places and objects are things so they inherit from physical.  
> Hence, a forest is a place that can contain
> a sword (an object), a dragon (a person) or another place (a small house).  
> Persons can be contained in places
> or things (coffins).
> The problem is that the main game loop gets the current location by taking 
> the container of the hero... which is a physical.  However, it needs to 
> call the place-specific method "go".

I think I wouldn't use objects at all in this situation. You're trying
to model the location-tree of "physical" things. A tree must be uniform
in its contents, but you want to put different things into it. That
means you must use an algebraic type, or variants. I'd probably either
use a zipper, or connect the tree nodes with "refs" for the non-pure

Attaching operations like "go" to a specific object is also a leftover
from the "everything is an object" philosophy. If you must move a physical
thing, just move it. If you need to attach special behaviour for some
sort of movements, find out what it depends on (source, destination,
thing moved, or a combination) and handle those cases in the move-routine.

- Dirk