English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

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-03 (21:09)
From: Goswin von Brederlow <goswin-v-b@w...>
Subject: Execution order in class construction

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

Will that always give x < y < z or could it initialize the values in
different order?

In my use case I want to parse the values from a stream and have
inheritance between classes.

class foo stream = object
  inherit base1 stream
  inherit base2 stream
  val x = parse_x stream
  val y = parse_y stream

Will that always execute in order? If the order is undefined then that would
make things more complex and require additional type definitions:

class foo stream =
  let base1_temp = parse_base1 stream in
  let base2_temp = parse_base2 stream in
  let x_temp = parse_x stream in
  let y_temp = parse_y stream
      inherit base1 base1_temp
      inherit base2 base2_temp
      val x = x_temp
      val y = y_temp