Version française
Home     About     Download     Resources     Contact us    
Browse thread
Constructeurs en O'Caml
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Christian Boos <boos@a...>
Subject: Re: Constructeurs en O'Caml

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