Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

.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

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

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)

like image 691
lstachowiak Avatar asked Nov 19 '20 12:11

lstachowiak


People also ask

What does ⟨F⟩ mean?

This sound is usually considered to be an allophone of /h/, which is pronounced in different ways depending upon its context; Japanese /h/ is pronounced as [ɸ] before /u/. In Welsh orthography, ⟨f⟩ represents /v/ while ⟨ff⟩ represents /f/. In Slavic languages, ⟨f⟩ is used primarily in words of foreign (Greek, Latin, or Germanic) origin.

What does the letter F mean in math?

In countries such as the United States, the letter "F" is defined as a failure in terms of academic evaluation. Other countries that use this system include Saudi Arabia, Venezuela, and the Netherlands. In the hexadecimal number system, the letter "F" or "f" is used to represent the hexadecimal digit fifteen (equivalent to 15 10 ).

What does F stand for in the Etruscan alphabet?

In the Etruscan alphabet, 'F' probably represented /w/, as in Greek, and the Etruscans formed the digraph 'FH' to represent /f/.

Is the letter F doubled at the end of words?

It is often doubled at the end of words. Exceptionally, it represents the voiced labiodental fricative / v / in the common word "of". F is the twelfth least frequently used letter in the English language (after C, G, Y, P, B, V, K, J, X, Q, and Z ), with a frequency of about 2.23% in words.


2 Answers

The problem is that .NET 5 added an overload. Before there was only TryRemove (key : 'a, byref<'b> value) : bool, now the new overload TryRemove(item: KeyValuePair<'a, 'b>) : bool gets chosen. See netcore 3.1 vs NET 5

An alternative solution is to add a type annotation, e.g.

let tryRemove (key: 'a) (dict: Concurrent.ConcurrentDictionary<'a, 'b>) =
   match dict.TryRemove(key) with
   | (true, v) -> Some v
   | (false, _) -> None
like image 176
CaringDev Avatar answered Nov 15 '22 07:11

CaringDev


I have just figured out the:

let mutable v = Unchecked.defaultof<'b>

instead of

let mutable v: 'b = null

works, but it's super strange the simplified syntax with last out argument translated to tuple result does not work anymore. Does it?

EDIT
It still works! See the right answer :)

like image 25
Witold Szczerba Avatar answered Nov 15 '22 08:11

Witold Szczerba