From: Didier.Remy@inria.fr (Didier Remy)
To: Pierre.Casteran@labri.u-bordeaux.fr (Pierre CASTERAN)
Cc: caml-list@inria.fr
Subject: Re: about "new"
Date: Fri, 25 Jul 1997 13:54:36 +0200 (MET DST) [thread overview]
Message-ID: <199707251154.NAA14258@pauillac.inria.fr> (raw)
In-Reply-To: <33D86D30.5889@labri.u-bordeaux.fr> from Pierre CASTERAN at "Jul 25, 97 11:09:04 am"
> 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.
prev parent reply other threads:[~1997-07-25 13:54 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
1997-07-25 9:09 Pierre CASTERAN
1997-07-25 10:02 ` Francois Pessaux
1997-07-25 11:54 ` Didier Remy [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=199707251154.NAA14258@pauillac.inria.fr \
--to=didier.remy@inria.fr \
--cc=Pierre.Casteran@labri.u-bordeaux.fr \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox