Re: about "new"

Francois Pessaux (pessaux@pauillac.inria.fr)
Fri, 25 Jul 1997 12:02:05 +0200

Message-Id: <199707251002.MAA12828@pauillac.inria.fr>
To: Pierre.Casteran@labri.u-bordeaux.fr, caml-list@inria.fr
Subject: Re: about "new"
Date: Fri, 25 Jul 1997 12:02:05 +0200
From: Francois Pessaux <pessaux@pauillac.inria.fr>

> Is there any way to define a class with some initializing function,
> which is to be called each time an instance is created by a "new" ;
> the interest would be of course to create a "consistent" state for
> a newly created object.
Just after the object creation ? For this I've no idea.

> Similarly, is it possible to attach a function to a class, which
> would be called before the creation of an instance ? it would be
> useful to check some preconditions to the creation of an instance.
> we can imagine that, if that function returns false, an exception
> is raised and the instance is not created.

For this, a solution would be to create a dummy value field whose value i=
s
the code for your "constructor-function". For example :

class non_null_line x1 y1 x2 y2 =3D
val private assertion =3D if x1 =3D x2 && y1 =3D y2 then failwith "Null=
line"
val start_pt =3D (x1, y1)
val end_pt =3D (x2, y2)
...
end
;;

This class create line objets whith a non 0 length. Each time an object i=
s
created, the value for private is computed, and if it goes wrong, an exce=
ption
is raised, avoiding the object creation.
We can "privatize" the "assertion" field in order to to see it in the cla=
ss
type. In fact, this method depends on fields evaluation order of course..=
=2E
So if you use side effets in previous fields, depending on the evaluation=

order, they will be took in account or not. I know this solution ss a bit=

hacking ;-)

-- =

(* Francois PESSAUX (Francois.Pessaux@inria.fr) *)
(* INRIA Rocquencourt - Projet CRISTAL *)
(* (http://pauillac.inria.fr/~pessaux) *)
;;