Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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: 2009-04-06 (04:42)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] Execution order in class construction
From: Goswin von Brederlow <>

> 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