【发布时间】:2020-11-19 12:03:36
【问题描述】:
我正在将我的 F# 代码从 dotnet3.1 迁移到 5,并在以下代码中苦苦挣扎:
let tryRemove key (dict: Concurrent.ConcurrentDictionary<'a, 'b>) =
match dict.TryRemove(key) with
| (true, v) -> Some v
| (false, _) -> None
在 3.1 TryRemove 返回元组,在版本 5 中它只返回布尔值。要从字典中获取值,我需要将引用作为TryRemove 的第二个参数传递。
正确的做法是什么,避免返回 null v?
我尝试了以下代码:
let tryRemove key (dict: Concurrent.ConcurrentDictionary<'a, 'b>): 'b option =
let mutable v: 'b = null
match dict.TryRemove(key, &v) with
| true -> Some v
| _ -> None
但是现在使用它的函数认为 tryRemove 的 Option 内可能有 null
错误 FS0001:类型 '(Body -> unit)' 没有 'null' 作为正确值
在哪里b' is (Body -> unit)
【问题讨论】: