Zitat von Tim Leonard <Tim@timleonard.us> (Tue, 13 Feb 2018 22:31:14 -0500)
A simple question of syntax: why does the first definition of function f cause a syntax error?
Shouldn’t the semicolon syntactically terminate the match expression?
No.
You can use semicolon to put more then one expression together.
So the "field2 = 2" is seen as part of the match.
If you have a pattern matching, this way you can put multiple commands/expressions in a row,
without the need to use begin/end or ( ) in any match-case.
It's the other way around: you need to put begin/end or ( ) around a match-statement.
This way you have to add one such enclosing around a match-statement,
instead of one such enclosing in any match-case of such a statement.
type my_record = { field1 : bool; field2 : int };;
let f x = { field1 = match x with _ -> true ; field2 = 2 };; (* this fails *)
Here I get "Error: Unbound value field2",
which is, because the match-case reaches until the }.
let f x = { field1 = ( match x with _ -> true ); field2 = 2 };; (* this is ok *)
Here it works, because the match-statement is sorrounded / enclosed by ( and ).
Ciao,
Oliver
--
Caml-list mailing list. Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs