假设我有以下代码:使用不完整的模式匹配作为过滤器?
type Vehicle =
| Car of string * int
| Bike of string
let xs = [ Car("family", 8); Bike("racing"); Car("sports", 2); Bike("chopper") ]
我可以使用不完整的图案匹配过滤器上方列表中的当务之急样环:
> for Car(kind, _) in xs do
> printfn "found %s" kind;;
found family
found sports
val it : unit =()
,但它会引起:warning FS0025: Incomplete pattern matches on this expression. For example, the value 'Bike (_)' may indicate a case not covered by the pattern(s). Unmatched elements will be ignored.
由于忽视无与伦比的元素是我的意图,是否有可能摆脱这种警告?
有没有办法使这项工作与列表理解而不会导致MatchFailureException?例如类似的东西:
> [for Car(_, seats) in xs -> seats] |> List.sum;;
val it : int = 10
我认为这条线假设是:[公共汽车(_,座位)在xs - >席位] |> List.sum ;;对? ;) – 2011-04-11 23:52:01
哦,我明白了! 2辆车! 5 x 2 = 10!主啊,帮助我。 – 2011-04-12 00:03:27
这是一辆家庭轿车和一辆跑车,所以8 + 2 = 10。 – Laurent 2011-04-12 00:05:27