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-27 (09:30)
From: Dirk Thierbach <dthierbach@g...>
Subject: Re: [Caml-list] Objects, dynamic cast, Obj.magic abuse and dragons
On Tue, Feb 26, 2008 at 05:25:05PM +0100, Berke Durak wrote:
> Dirk Thierbach wrote :
>> 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
>> version. 

> You are preaching to the choir.  

I'm not preaching here :-)

> I'm not Gerd Stolpmann and I don't use objects very much because
> every time I try I get seven-page-long errors.

I, OTOH, use them frequently. I also get seven-page-long errors,
but usually I can find the bug nevertheless :-)

> However it seems that objects could be actually useful in situations
> where you have a hierarchy of objects with behaviours, such as a graphical
> user interface made of widgets, or, say, a text adventure.

Yes, they can be very useful in some situations, especially in GUIs.
However, I think they are not useful in the particular situation of
a text adventure.

And I think that the criterion is not having a hierarchy of behaviours
(first, "inheritance is not subtyping", second, as you've seen, you
can run into trouble with static type checking easily), but the criterion
is having substantial state that should be partitioned and decoupled.

This also applies to the text adventure to some degree, but the actual
state is much simpler: Must items will have no state at all, or
at most a boolean like "open/closed". Their *relationship*, OTOH
(i.e. location, attachment) can be described uniformly and globally.
Also, for the actual actions, you need multi-dispatch: The outcome
of the action depends on the verb and multiple items ("tie rope to
railing"). And all this says that single-dispatch statically typed 
objects, like in OCaml, are a bad match for this situation.

- Dirk