开发者

'option<unit->unit> type and the equality' using F#

Consider th开发者_开发技巧e following code:

let fnOption = Some (fun () -> ())
fnOption = None

It gives the following error:

init.fsx(2,1): error FS0001: The type '(unit -> unit)' does not support the 'equality' constraint because it is a function type

Why is that? Did I overlook something?


If you really wanted to check for equality (and not to assign the value), then you can use Option.isNone fnOption. If you really do want to assign, look as kvb's answer.


It's unclear what you're trying to do. As it stands, the second line is an equality test, not an assignment. If what you intended to do was assign a new value to fnOption, then you need to make it mutable:

let mutable fnOption = Some(fun () -> ())
fnOption <- None

If you are really trying to test equality, then the error you are seeing is the expected result, because functions aren't comparable.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜