Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# Add New Item to Collections.Map?

I want to create a map and a function to add item to that map.

This is what I did

let mymap = Map.empty

let myfunc nId nValue = 
    mymap = Map.add nId nValue ;;

But this produced following Error
This expression was expected to have type Map<'a,'b> but here has type Map<'c,'d> -> Map<'c,'d>

What did I do wrong?

like image 219
Michael Yuwono Avatar asked Dec 05 '22 01:12

Michael Yuwono


1 Answers

Maps are immutable so you need to do let mutable mymap.

Also, = does comparison, you need <- for assignment, which is why you got the error.

Something like

let mutable mymap = Map.empty

let myfunc nId nValue = 
    mymap <- Map.add nId nValue mymap;;

is what you want

like image 75
John Palmer Avatar answered Jan 10 '23 19:01

John Palmer