Hi,
Maybe defining is not the right word here. An alternative
explanation would be that "let" binds variables in the pattern on
the left in order to match the expression on the right. For
instance, the following exotic expression:
# type alt = Left of int | Right of int
# let (Left a|Right a) = Left 0;;
- : val a: int
is valid and binds the variable "a" to the value 0.
Since there is no variable to be bound in "()" or any constructor
without argument,
#let () = ();;
just computes the expression on the right and does not bind
anything on the left.
Note that in some way, the toplevel is already telling you that,
since
# let () = ();;
has no output compared to
# ();;
- : unit = ()
which prints the value of the constructor ().
− octachron
Le 03/18/16 19:04, Mr. Herr a écrit :
Hi,
in a small presentation of OCaml (Linux User Group Bremen) I got
some interesting questions, and
trying to answer I noticed I took something for granted that was
not fully understood.
Looking at this in the toplevel:
# let () = () ;;
# () ;;
- : unit = ()
# let _ = () ;;
- : unit = ()
# let None = None;;
Warning 8: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
Some _
#
... the question is: okay, pattern matching left of the equal
sign, but what does it define?
It defines unit and None in the environment, and then that value
is just sitting there?
/Str.