Re: Dichotomy between functions and type constructors?

From: Markus Mottl (mottl@miss.wu-wien.ac.at)
Date: Mon Feb 21 2000 - 20:31:07 MET

  • Next message: Markus Mottl: "Re: Still strange GC problems with OCaml and C: OCaml 2.04 bug?"

    > 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
    



    This archive was generated by hypermail 2b29 : Tue Feb 22 2000 - 11:48:17 MET