Version française
Home     About     Download     Resources     Contact us    
Browse thread
Execution order in class construction
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Goswin von Brederlow <goswin-v-b@w...>
Subject: Re: [Caml-list] Execution order in class construction
Jacques Garrigue <garrigue@math.nagoya-u.ac.jp> writes:

> From: Goswin von Brederlow <goswin-v-b@web.de>
>
>> I'm wondering if the execution order is defined during class
>> construction. For example:
>> 
>> let n = ref 0
>> let next () = incr n; !n
>> class foo = object
>>   val x = next ()
>>   val y = next ()
>>   val z = next ()
>>   method print = Printf.printf "%d %d %d\n" x y z
>> end
>> 
>> Will that always give x < y < z or could it initialize the values in
>> different order?
>
> This is not explicitly specified in the manual (but not explicitly
> left unspecified either). The same thing seems to be true for
> initializers too.

Things like that should really be specified. In that regard the docs
are saddly lacking.

With a specified in order execution the following could even be allowed:

class foo buffer = object
  val num = int_from_buffer buffer
  val data = Array.init num (float_from_buffer buffer)
end

> The current implementation keeps the definition order, and I don't see
> why it should change, but if your code depends on such things it may
> always be a good idea to put somewhere a bit of code that verifies
> that the behaviour is correct.

How would you verify that? If the order is unspecified then it might
just give x < y < z in one case and x > y > z for a slightly different
case where the compiler optimized differently. I would have to verify
every single class where the order is important.

> Jacques Garrigue

MfG
        Goswin