From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id PAA15903 for caml-redistribution; Fri, 25 Jul 1997 15:52:49 +0200 (MET DST) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id MAA12833 for ; Fri, 25 Jul 1997 12:02:10 +0200 (MET DST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.8.5/8.8.5) with ESMTP id MAA28623; Fri, 25 Jul 1997 12:02:07 +0200 (MET DST) Received: from localhost (localhost [127.0.0.1]) by pauillac.inria.fr (8.7.6/8.7.3) with SMTP id MAA12828; Fri, 25 Jul 1997 12:02:06 +0200 (MET DST) Message-Id: <199707251002.MAA12828@pauillac.inria.fr> X-Authentication-Warning: pauillac.inria.fr: Host localhost [127.0.0.1] didn't use HELO protocol X-Mailer: exmh version 1.6.4 10/10/95 To: Pierre.Casteran@labri.u-bordeaux.fr, caml-list@inria.fr Subject: Re: about "new" In-reply-to: Your message of "Fri, 25 Jul 1997 11:09:04 +0200." <33D86D30.5889@labri.u-bordeaux.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable Date: Fri, 25 Jul 1997 12:02:05 +0200 From: Francois Pessaux Sender: weis > 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 is the code for your "constructor-function". For example : class non_null_line x1 y1 x2 y2 = val private assertion = if x1 = x2 && y1 = y2 then failwith "Null line" val start_pt = (x1, y1) val end_pt = (x2, y2) ... end ;; This class create line objets whith a non 0 length. Each time an object is created, the value for private is computed, and if it goes wrong, an exception is raised, avoiding the object creation. We can "privatize" the "assertion" field in order to to see it in the class type. In fact, this method depends on fields evaluation order of course... 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) *) ;;