Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Question
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: sebastien FURIC <sebastien.furic@t...>
Subject: [Caml-list] Question
 Hi,

 What do you think of the following code?

# type toto = Toto of int | Titi of string;;
type toto = Toto of int | Titi of string
# let test t t' = match t, t' with
  | ((Toto _ | Titi _), Toto x | Toto x, (Toto _ | Titi _)) when x = 0
-> "OK"
  | _ -> "KO";;
Characters 73-79:
Warning: this pattern is unused.
  | ((Toto _ | Titi _), Toto x | Toto x, (Toto _ | Titi _)) when x = 0
-> "OK"
                                          ^^^^^^
val test : toto -> toto -> string = <fun>
# test (Toto 0) (Toto 1);;
- : string = "KO"

 I was expecting "when" to be right distributive over "|". I find
OCaml's behaviour not very intuitive in such a situation. The correct
code is:

# let test t t' = match t, t' with
  | (Toto _ | Titi _), Toto x when x = 0 -> "OK"
  | Toto x, (Toto _ | Titi _) when x = 0 -> "OK"
  | _ -> "KO";;
val test : toto -> toto -> string = <fun>
# test (Toto 0) (Toto 1);;
- : string = "OK"

 Is there a good reason for this?

 Cheers,

 Sébastien.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners