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 (13:10)
From: Berke Durak <berke.durak@e...>
Subject: Re: [Caml-list] Objects, dynamic cast, Obj.magic abuse and dragons
I have found a where, for each category C (such as place or person), you add a method

   as_C : C

in physical that throws a Class_cast_exception, and override it with a method that returns
self in class C.  However this means that the type C must appear in the definition of physical,
which means that either

   (a) All categories C1, ..., Cn are defined in the same file in the same bunch
of mutually-recursive class definitions; a solution evidently not scalable.

   (b) The physical class is parametrized by n paramters 'C1, 'C2, ... 'Cn, which
must be repeated everywhere.

The latter solution works for small n but the complexity of incremental maintenance is in O(n).
This means that if you define your n classes in n files, you'll have to edit n files to add
an (n+1)-th class.

This leads me back to an idea I was talking about with Yann Régis-Gianas a few months ago :
the ability to bundle type parameters as a named record and to access their components.

You could write, in a file
   type ''bundle := ('place, 'person, 'c3, 'c4 ...)

then in :

   class [F.''bundle] physical =
       method as_place : raise Class_cast_exception
       method as_person : raise Class_cast_exception
       method as_c3 : raise Class_cast_exception

and in
   class [F.''bundle] place =
     object(self : 'a)
       constraint ''bundle.'place = 'a
       method as_place = self

and so on...  I don't know how much sense this makes with respect to separate compilation.
However, this would allow you to add a category by just editing two files.