Skip to content

Instantly share code, notes, and snippets.

@vogler
Last active December 14, 2016 14:01
Show Gist options
  • Save vogler/4f57327cf735a329553c6a109fa5b3b5 to your computer and use it in GitHub Desktop.
Save vogler/4f57327cf735a329553c6a109fa5b3b5 to your computer and use it in GitHub Desktop.

This extension helps dealing with warning 57:

File "when.ml", line 28, characters 4-19:
Warning 57: Ambiguous or-pattern variables under guard;
variable x may match different arguments. (See manual section 8.5)

Problem

let f = function
  | A x, _ (* A 0, A 1 would match here and fail the guard *)
  | _, A x when x<>0 -> 1 (* warning 57 *)
  | _ -> 2
(* sadly no easy way to have the guard checked for every pattern: *)
let f = function
  | A x, _ when x<>0 -> 1
  | _, A x when x<>0 -> 1 (* if these were big expressions, we would need to pull out a function for each case to avoid duplication *)
  | _ -> 2

Solution

Different possibilities (see when.ml). Decided on the following:

let f = function%distr (* via [ppx_ext_expr.ml]: applies to all cases, no brackets, does not compile w/o ppx *)
  | A x, _ | _, A x when x<>0 -> 1
  | _ -> 2

Which will result in

let f = function
  | A x, _ when x<>0 -> 1 | _, A x when x<>0 -> 1
  | _ -> 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment