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: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] Execution order in class construction
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.

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.

Jacques Garrigue