Re: Constructeurs en O'Caml

Christian Boos (boos@arthur.u-strasbg.fr)
Wed, 9 Oct 1996 12:26:31 +0200

Date: Wed, 9 Oct 1996 12:26:31 +0200
Message-Id: <199610091026.MAA11690@arthur.u-strasbg.fr>
From: Christian Boos <boos@arthur.u-strasbg.fr>
To: Vyskocil Vladimir <Vladimir.Vyskocil@sophia.inria.fr>
Subject: Re: Constructeurs en O'Caml
In-Reply-To: <199610090925.LAA15108@psyche.inria.fr>

Vyskocil Vladimir writes:
> Existe-t'il comme en C++ une fonction qui est appelee a la construction d'un
> objet (pas seulement pour initialiser des variables d'instances) ?

No, there's nothing such a "constructor" method, but a way to circumvent this
is to do the job as a side effect, when initializing the members. See the fol-
lowing sample code :

class my_class name =
(* MEMBERS *)

val n =
(* begin constructor *)
...;
(* end constructor *)
name

(* METHODS *)
...
end

I would suggest a clearer syntax for that, by the mean of anonymous members :

val _ = (* constructor code *)

Of course, these members will not be real fields in the object instances,
but just "fake" fields for the purpose of calling constructors. In that way,
you could have constructors for objects with no members.

-- Christian Boos