F# - Using Concurrent.ConcurrentDictionary.TryRemove with dotnet 5

lst*_*iak 6 .net f# c#-to-f#

I'm migrating my F# code from dotnet3.1 to 5 and struggling with following code:

        let tryRemove key (dict: Concurrent.ConcurrentDictionary<'a, 'b>) =
           match dict.TryRemove(key) with
           | (true, v) -> Some v
           | (false, _) -> None
Run Code Online (Sandbox Code Playgroud)

In 3.1 TryRemove returned tuple, in version 5 it returnes only boolean value. To get value from dictionary I need to pass reference as second parameter of TryRemove. What is the correct way to do it and avoid returning null v?

I have tried following code:

    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
Run Code Online (Sandbox Code Playgroud)

But now function that uses it thinks that it is possible to have null inside that Option from tryRemove

error FS0001: The type '(Body -> unit)' does not have 'null' as a proper value

where b' is (Body -> unit)

Car*_*Dev 7

问题是 .NET 5 添加了一个重载。之前只有TryRemove (key : 'a, byref<'b> value) : bool,现在TryRemove(item: KeyValuePair<'a, 'b>) : bool选择了新的重载。参见netcore 3.1NET 5

另一种解决方案是添加类型注释,例如

let tryRemove (key: 'a) (dict: Concurrent.ConcurrentDictionary<'a, 'b>) =
   match dict.TryRemove(key) with
   | (true, v) -> Some v
   | (false, _) -> None
Run Code Online (Sandbox Code Playgroud)