以下代码在VS2012中不能通过编译,你知道么?知道为什么么?
let f0 x = x % 2 = 0
let f1 x = x % 2 <> 0
let ( | Even | _ | ) f (x:int) = if f(x) thenSome(Math.Sign(x)) else None
let ( | Odd | _ | ) f (x:int) = if f(x) thenSome(Math.Sign(x)) else None
let f (x:int) =
match x with
|Even (fun x ->x % 2 = 0) sign-> sprintf "even sign=%d" sign
| Odd f1 sign-> sprintf "odd sign=%d" sign
编译器会在关键字”fun”那里报一个错误,抱怨“Unexpected keyword ‘fun’ inpattern. Expected ‘)’ or other token”。见如下截图:
你有想过这个编译错误仅仅是因为我们不能在模式里面放入lambda表达式么?如果是这样的话,那么恭喜你,你猜对了!J
似乎这个错误信息会误导我们去写很多的“)”或者其它的一些疯狂的绕过去的举措。如果编译器直接告诉用户我们不支持它那就好了。不管怎样,如果你知道了这个点,那么我们就不会陷入困境了。