1

给定以下活动模式:

let (| HasMatch |) (x:string) = 
  if x.Contains("0") then Some()
  else None;;

以及以下模式匹配函数:

let testFn x = function
  | HasMatch i -> printfn "%A" i
  | _ -> printf "nope";;

最后一行的通配符模式说warning FS0026: This rule will never be matched

我看到的所有示例似乎都推断出部分活动模式必须返回Some('a)以匹配,并且返回的模式None被通配符捕获。错误似乎有不同的说法。

我错过了什么?

4

1 回答 1

3

我认为您应该将None案例添加到活动模式声明中,如下所示:

let (| HasMatch | _ |) (x:string) = 
  if x.Contains("0") then Some()
  else None;;

在您的原始示例中,编译器推断您实际上想要返回Option类型。当您printf在示例中运行时,您会看到它Some Null在匹配时打印。

另外,退货不好Some(),您应该退货 saySome(x)或类似的

于 2013-07-21T21:47:50.077 回答