Search code examples
haskellhashmapalgebraic-data-typessum-type

Sum type with HashMap


The following code triggers an error:

import           Data.HashMap.Strict (HashMap) -- from unordered-containers
import           Data.Text

data Value =
    VText Text
  | VList [Text]
  | VMap HashMap Text Text
  deriving Show

The compiler complains that:

Expecting two more arguments to ‘HashMap’
  Expected a type, but ‘HashMap’ has kind ‘* -> * -> *’
  In the type ‘HashMap’
  In the definition of data constructor ‘VMap’
  In the data declaration for ‘Value’typecheck

How can I create a sum type like Value where one of the constructors takes a HashMap Text Text?


Solution

  • By using parenthesis to disambiguate parameters for HashMap in VMap:

    data Value =
        VText Text
      | VList [Text]
      | VMap (HashMap Text Text)
      deriving Show