Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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

f.ml:
   type ''bundle := ('place, 'person, 'c3, 'c4 ...)

then in physical.ml :

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

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

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.
-- 
Berke DURAK