开发者

F# multiple when guards using grouping patterns in pattern matching

why this code does 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 -> doSometh开发者_StackOverflowing()
  ...

So is it possible to have several when guards in pattern groups?


You can only have one when guard per arrow/result, so something like this would work:

let id1 = 0

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

(note in this case both items in the or must bind the same set of identifiers so that in either case no identifer is left uninitialized)

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()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜