Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: Execution time of class versus record
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: David Allsopp <dra-news@m...>
Subject: Re: Execution time of class versus record
On Jun 26, 2007, at 08:53 AM, Loup Vaillant wrote:
> > For the same reason, there is a difference between:
> >
> >   type t = A of int * int
> >
> > and:
> >
> >   type t = A of (int * int)
>
> Err, I don't get it : I see exactly the same thing (written twice) here.
>
> Are you telling that :
> type t = A of int * int   <==>   type t = (A of int) * int
>
>?

The type declarations are different - the top-level reports a different type
in each case!

The brackets matter because they tell O'Caml how many *arguments* the
constructor A will take and so affect the internal representation.

With the first type declaration, A(1, 2) is a 2-argument constructor and the
value will be represented internally as a 2-field block with tag 0 with
field 0 containing 1 and field 1 containing 2. This means that this code
wouldn't work:

    let foo = (1, 2)
    in
      A foo

but with the second type declaration it would.

With the second type declaration, A (1, 2) is a 1-argument constructor and
will be represented internally as a 1-field block with tag 0 with field 0
"pointing" to another 2-field block containing the 1 and 2.

Section #18.3.4 of the manual explains the internal representation
difference (esp. with ref to #18.3.6 where it is compared to Polymorphic
Variants) but I was sure that there was an explicit reference in the manual
to the difference between the type declarations but I can't find it now :(

The confusion arises because the notation for arguments of a constructor and
tuple construction look the same - I believe the "revised" syntax for O'Caml
changes the way these are declared to make it clearer but I've never looked
at it...


David