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:
 > 
 > [ ... see my previous message]
 > 
 > Yes, but such contructor can't call a object method because self is
 > bind only at execution ie I can't do the following :
 >
 > [ ... ]

Agreed, this is because you're not allowed to send messages to a partially
constructed object.

Solutions may be:

1) the 'val _ = ...' I suggested should be evaluated after all others
member initializations. [self] can then be safely bound inside the right part.

Pro: the 'val _' is like any other val, i.e it contains code evaluated
     at object creation time.

Cons: the 'val _' is the only member where self is bound.


2) as you suggested it, a constructor method can do the job
 (either named [init] or [my_class], the later would recall C++ ...).

Pro: self is bound in the 'method init = ...' as it is for all methods.

Cons: the 'init' method is called at object creation time, and you should not
     be allowed to call it directly.


3) Any other idea ?


But for sure, constructors are an usefull idiom !

-- Christian Boos