我对 F# 和活动模式还很陌生,我遇到了一个我无法解释的异常情况。
module Eval =
let (|Bet|Pass|) (test:BetChoice) =
match test with
| BetChoice.Bet -> Bet
| BetChoice.Pass -> Pass
let (|NoBet|Bet|Pass|) (test:Nullable<BetChoice>) : Choice<unit, unit, unit> =
match test.HasValue with
| true -> match test.Value with
| BetChoice.Bet -> Bet
| BetChoice.Pass -> Pass
| false -> NoBet
let FlipByWinner ((value:int), (awins:bool)) =
match awins with
| true -> (value, -value)
| false -> (-value, value)
let Evaluation (awins:bool) (player11:BetChoice) (player21:BetChoice) (player12:Nullable<BetChoice>) =
match player11 with
| Pass -> match player21 with
| Pass -> FlipByWinner(1, awins)
| Bet-> match player12 with
| Bet -> FlipByWinner(2, awins)
| Pass -> FlipByWinner(1, false)
| NoBet -> raise (System.ArgumentException("invalid strategy"))
| Bet -> match player21 with
| Bet -> FlipByWinner (2, awins)
| Pass -> FlipByWinner (1, false)
这不编译。通过一个小的调整,我可以让它按预期工作,但我不知道到底发生了什么让我有点紧张......第二种模式可以重命名为“(|NoBet|Bet1|Pass1| )" 并且它的关联模式在整个代码中都发生了变化,然后它就可以工作了,但我真的不明白为什么会有类型不匹配异常。
还有一种处理几乎相同但不完全相同的 2 个活动模式的好方法吗?似乎应该有一种方法可以将常见的东西放在一起。(作为旁注,看起来缩进在复制/粘贴中搞砸了,这完全是模块 Eval 的一部分)。