caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Execution order in class construction
@ 2009-04-03 21:09 Goswin von Brederlow
  2009-04-06  4:41 ` [Caml-list] " Jacques Garrigue
  0 siblings, 1 reply; 3+ messages in thread
From: Goswin von Brederlow @ 2009-04-03 21:09 UTC (permalink / raw)
  To: caml-list

Hi,

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?

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
end

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
  in
    object
      inherit base1 base1_temp
      inherit base2 base2_temp
      val x = x_temp
      val y = y_temp
    end

MfG
        Goswin


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Execution order in class construction
  2009-04-03 21:09 Execution order in class construction Goswin von Brederlow
@ 2009-04-06  4:41 ` Jacques Garrigue
  2009-04-06  6:12   ` Goswin von Brederlow
  0 siblings, 1 reply; 3+ messages in thread
From: Jacques Garrigue @ 2009-04-06  4:41 UTC (permalink / raw)
  To: goswin-v-b; +Cc: caml-list

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Execution order in class construction
  2009-04-06  4:41 ` [Caml-list] " Jacques Garrigue
@ 2009-04-06  6:12   ` Goswin von Brederlow
  0 siblings, 0 replies; 3+ messages in thread
From: Goswin von Brederlow @ 2009-04-06  6:12 UTC (permalink / raw)
  To: Jacques Garrigue; +Cc: goswin-v-b, caml-list

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-04-06  6:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-04-03 21:09 Execution order in class construction Goswin von Brederlow
2009-04-06  4:41 ` [Caml-list] " Jacques Garrigue
2009-04-06  6:12   ` Goswin von Brederlow

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).