Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: Dichotomy between functions and type constructors?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Markus Mottl <mottl@m...>
Subject: Re: Dichotomy between functions and type constructors?
> Say I have a type
> 
>    T1 of int * float

> I can create such a type as T1(5, 3.14) but I cannot create such a type on a
> tuple result of a function call:
> 
> let doit x = (x, float_of_int x)
> 
> let myT1 = T1(doit x)

This is a trap into which I have already fallen when I tried to find a bug
in a C-interface. Xavier explained the "problem" to me when I sent a bug
report, because I had thought that the source of the error originated in
OCaml:

  type t = T1 of int * float

is *not* the same as:

  type t = T1 of (int * float)

The first one is an example of a dataconstructor that takes two arguments
(an int and a float), whereas the second one is a dataconstructor that
takes *one* argument which happens to be a tuple...

So this, for example, works:

  type t = T1 of (int * float)

  let doit x = (x, float_of_int x)
  let myT1 x = T1(doit x)

I haven't taken a look at the FAQs, whether this misunderstanding is
already explained there. In any case, it should be mentioned somewhere,
because it is a somewhat surprising fact (if you haven't seen it before).
It is especially difficult to find the bug if you create data in C the
wrong way and wonder why the program keeps crashing: in contrast to the
multi-argument case the tuple case is represented internally with an
indirection...

Best regards,
Markus Mottl

-- 
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl