* about "new"
@ 1997-07-25 9:09 Pierre CASTERAN
1997-07-25 10:02 ` Francois Pessaux
1997-07-25 11:54 ` Didier Remy
0 siblings, 2 replies; 3+ messages in thread
From: Pierre CASTERAN @ 1997-07-25 9:09 UTC (permalink / raw)
To: caml-list
Hello,
I'm a beginner in Objective Caml, but not in ML; my question is about
object building :
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.
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.
--
Pierre Casteran,
LaBRI, Universite Bordeaux-I | 12 place Puy Paulin
351 Cours de la Liberation | 33000 Bordeaux
F-33405 TALENCE Cedex | France
France | (+ 33) 5 56 81 15 80
tel : (+ 33) 5 56 84 69 31
fax : (+ 33) 5 56 84 66 69
email: casteran@labri.u-bordeaux.fr
www: http://dept-info.labri.u-bordeaux.fr/~casteran
"Les rêves sont aussi beaux que la réalité, mais ils ne sont pas mieux".
( J.L.Borges )
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: about "new"
1997-07-25 9:09 about "new" Pierre CASTERAN
@ 1997-07-25 10:02 ` Francois Pessaux
1997-07-25 11:54 ` Didier Remy
1 sibling, 0 replies; 3+ messages in thread
From: Francois Pessaux @ 1997-07-25 10:02 UTC (permalink / raw)
To: Pierre.Casteran, caml-list
> 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) *)
;;
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: about "new"
1997-07-25 9:09 about "new" Pierre CASTERAN
1997-07-25 10:02 ` Francois Pessaux
@ 1997-07-25 11:54 ` Didier Remy
1 sibling, 0 replies; 3+ messages in thread
From: Didier Remy @ 1997-07-25 11:54 UTC (permalink / raw)
To: Pierre CASTERAN; +Cc: caml-list
> I'm a beginner in Objective Caml, but not in ML; my question is about
> object building :
> 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.
There are no such things as initialization methods defined in classes to
create new objects.
The only way to create an object from a class is the "new" construct.
You can always guarantee consistency by defining your own creation function.
For instance, assume you have defined the a class of integer values
class integer x as self =
val repr = x
method incr = {< repr = repr + 1 >}
method print = print_int repr
end;;
and that you want to ensure that repr is always positive.
You may defined
let real_integer x =
if x > 0 then new integer x
else raise (Failure "negative")
;;
and use real_integer x instead of new integer x to create integer objects.
real_integer 1;;
real_integer 0;;
However, consistency checks being written outside of classes will not
be inherited that way. A better solution is then to put them inside the
class.
class real_integer x =
inherit integer x
val repr = if x > 0 then x else raise (Failure "negative")
end;;
new real_integer 1;;
new real_integer 0;;
They will be inherited.
However, this way they is a unique view of security.
You need several subclass to implement several views of security.
Another possible solution is to add methods for checking consistency:
class real_integer x as self =
inherit integer x
method check =
if repr > 0 then self else raise (Failure "negative")
end;;
(new real_integer 1)#check;;
(new real_integer 0)#check;;
This allows several checks, but make consistency checking visible methods
of the object. This can be hidden a posteriori:
let real_integer x = ((new real_integer x)#check : integer);;
real_integer 1;;
real_integer 0;;
Hope this helps,
--Didier.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~1997-07-25 13:54 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-07-25 9:09 about "new" Pierre CASTERAN
1997-07-25 10:02 ` Francois Pessaux
1997-07-25 11:54 ` Didier Remy
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox