Search code examples
daml

What is the purpose of M.empty in Daml Map function?


when am using Map function it requires to declare M.empty,if am missing into an error,what is definition behind out of this M.empty usage,how exactly Map function work in DAML.

Error getting like below.

** could not Match Excepted type Map Text ([a]) with actual Type Map Text ([A]) -> Map Text ([A]) **

when i added M.Empty it is not running into this Error.For my understanding can please give explanation for how Map works and what is the reason behind M.Empty


Solution

  • It would be easier to answer if you provided the erroneous code, but from what I can parse, it looks like you may be misunderstanding what Map.empty is. Map.empty is not a function, it is a value; specifically, it is a Map that has no content.

    Maps are sets of key/value pairs. One way to construct the Map that you want is to start with an empty Map and then add each of the elements that you want, one by one. Here is an example:

    module Main where
    
    import qualified DA.TextMap as Map
    
    testMaps = scenario do
      let m1 = Map.empty
      let m2 = Map.insert "United States" "USD" m1
      let m3 = Map.insert "France" "EUR" m2
      let m4 = Map.insert "United Kingdom" "GBP" m3
      assert (Map.lookup "France" m3 == Some "EUR")
    

    If, on the line

      let m3 = Map.insert "France" "EUR" m2
    

    you forgot m2, you would get the error message you are describing. What this error message means is "I was expecting to get a Map (the result of the Map.insert function call), but instead I got a function that expects a Map and returns another Map". This is because DAML has a feature called automatic currying; the function Map.insert is defined as taking a key, a value, and a Map, and returning a Map. But you can also think of it as taking a key, and returning a function that takes a value, which returns a function that takes a Map, which returns a Map. So if you're missing an argument, the specific expression is still valid, it just happens to return a function that still expects one more argument, rather than returning a value.