F # multiple when guards use grouping patterns in relation to a pattern

why does this code not work, and how to make it work?

let id1 = 0
match p1, p2 with
  | Fluid, Particle id2 when id = id2
  | Interface _, Particle id2 when id = id2 -> doSomething()
  ...

So, is it possible to have several when the guards are in groups of patterns?

+3
source share
1 answer

You can only have one when protection is on the arrow / result, so something like this will work:

let id1 = 0

match p1, p2 with
| Fluid, Particle id2
| Interface _, Particle id2 when id1 = id2 -> doSomething()
| _ ->  doSomething()

(note, in this case, both elements in or must connect the same set of identifiers, so that in any case the identifier is not initialized)

or alternatively add a second action / result:

match p1, p2 with
| Fluid, Particle id2 when id1 = id2 -> doSomething()
| Interface _, Particle id2 when id1 = id2 -> doSomething()
| _ ->  doSomething()
+8
source

Source: https://habr.com/ru/post/1756879/


All Articles