Defining exceptions with a tuple as an argument

I am trying to define an exception in OCaml that takes a pair of tuples as an argument. However, this situation does not work?

# exception Foo of string list * string list;; 
exception Foo of string list * string list
# let bar = (["a"], ["b"; "c"; "d"]);;
val bar : string list * string list = (["a"], ["b"; "c"; "d"])
# raise(Foo bar);;
Error: The constructor Foo expects 2 argument(s),
       but is applied here to 1 argument(s)

However, if I do, it works

# raise (Foo (["a"], ["b"; "c"; "d"]));;
Exception: Foo (["a"], ["b"; "c"; "d"]).

What a deal? Thank!

+5
source share
3 answers

You look at it wrong (although I will not blame you: at first it is very surprising). It might seem to you that the constructors follow the syntax Name of type, where part of the type follows the syntax of the normal type (which allows you to contain tuples).

In fact, tuples and constructors follow the same syntax: a constructor is just a tuple with a name in front of it:

tuple/constructor == [name of] type [* type] [* type] ...

, * , . , N , , , .

- . :

[TYPE] [POINTER] [POINTER] [POINTER]

. , , ( ), :

[TYPE] [POINTER]
           |
           v
          [TYPE] [POINTER] [POINTER] [POINTER]

, , (- ). , name of type * type , name of (type * type), * of , , .

, : name (arg,arg). , , . , , , ( ) .

.

+11

OCaml :

Constr(1,2,3) - , . , Constr((1,2,3)). , Constr(1,2,3) , Constr((1,2,3)) - , (). Constr(1,2,3) , , , .

: Constr(((1,2,3))) Constr((1,2,3)). Constr(((1,2,3))) (1,2,3), .

+5

Foo . .

# exception Foo of string list * string list;;
exception Foo of string list * string list
# let bar = (["a"], ["b"; "c"; "d"]);;
val bar : string list * string list = (["a"], ["b"; "c"; "d"])
# let a, b = bar in raise (Foo (a, b));;
Exception: Foo (["a"], ["b"; "c"; "d"]).

, , parens, .

# exception Foo of (string list * string list);;
exception Foo of (string list * string list)
# let bar = (["a"], ["b"; "c"; "d"]);;
val bar : string list * string list = (["a"], ["b"; "c"; "d"])
# raise (Foo bar);;
Exception: Foo (["a"], ["b"; "c"; "d"]).
+4
source

All Articles