Version française
Home     About     Download     Resources     Contact us    
Browse thread
How to raise_with_arg() a tuple?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Thomas Fischbacher <Thomas.Fischbacher@P...>
Subject: Re: [Caml-list] How to raise_with_arg() a tuple?

On Thu, 7 Jul 2005, Olivier Andrieu wrote:

>  > exception Test_exn of int * int
> 
> It's the usual gotcha with variant constructor that have multiple
> arguments. The exception you defines has two arguments, whereas 
> 
>  exception Test_exn of (int * int)
> 
> has only one argument -- a tuple. It's not the same thing.

Oh dear. Indeed, if I just put parens around int * int, then everything is fine.

I don't know how other readers on the list think about this, but somehow 
I consider this a pretty bad tripwire. After all, I would not have 
expected behaviour like this:

# type foo =
 | Foo1 of int * int
 | Foo2 of (int * int)
;;
      type foo = Foo1 of int * int | Foo2 of (int * int)
# Foo1 (2,3);;
- : foo = Foo1 (2, 3)
# Foo2 (2,3);;
- : foo = Foo2 (2, 3)
# let x = (2,3) in Foo1 x;;
Characters 17-23:
  let x = (2,3) in Foo1 x;;
                   ^^^^^^
The constructor Foo1 expects 2 argument(s),
but is here applied to 1 argument(s)
# let x = (2,3) in Foo2 x;;
- : foo = Foo2 (2, 3)
# Foo1 2 3;;
Characters 7-8:
  Foo1 2 3;;
         ^
Syntax error

While it is certainly nice that there is an OCaml equivalent (I presume) 
of what would be written in Haskell as:

data Foo =
    Foo1 (Int,Int)
  | Foo2 Int Int
    deriving Show

Main> :load /tmp/1.hs
Main> Foo1 (2,3)
Foo1 (2,3)
Main> Foo2 2 3
Foo2 2 3

(I did not know about this so far!), the syntax used by ocaml certainly is 
quite misleading. As well, it seems to be a bit un-orthogonal here: after 
all, I cannot do this in OCaml (while it is allowed in Haskell, and might 
also be quite useful occasionally):

# Foo1 2;;
Characters 0-6:
  Foo1 2;;
  ^^^^^^
The constructor Foo1 expects 2 argument(s),
but is here applied to 1 argument(s)

Just as a further question, not to be meant as a suggestion or wishlist 
item: would it be conceivable that, at some point in the future, all the 
ocaml tools that operate on .ml files could accept different 
official "syntax variants" (say, the present one and a cleaned up syntax 
to be defined in the future), and dispatch between parsers via some 
command line argument or argv[0]? So that minor issues with the syntax 
could be repaired, should they turn out to be a problem.

> actually it's ok to raise exception directly (ie, not through
> CAMLreturn), the runtime takes care of releasing the local GC roots.

Many thanks again for your explanation.

-- 
regards,               tf@cip.physik.uni-muenchen.de              (o_
 Thomas Fischbacher -  http://www.cip.physik.uni-muenchen.de/~tf  //\
(lambda (n) ((lambda (p q r) (p p q r)) (lambda (g x y)           V_/_
(if (= x 0) y (g g (- x 1) (* x y)))) n 1))                  (Debian GNU)