使用 HashMap 求和类型
Sum type with HashMap
以下代码触发错误:
import Data.HashMap.Strict (HashMap) -- from unordered-containers
import Data.Text
data Value =
VText Text
| VList [Text]
| VMap HashMap Text Text
deriving Show
编译器抱怨:
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
如何创建像 Value
这样的求和类型,其中一个构造函数采用 HashMap Text Text
?
通过使用括号消除 VMap
中 HashMap
的参数歧义:
data Value =
VText Text
| VList [Text]
| VMap (HashMap Text Text)
deriving Show
以下代码触发错误:
import Data.HashMap.Strict (HashMap) -- from unordered-containers
import Data.Text
data Value =
VText Text
| VList [Text]
| VMap HashMap Text Text
deriving Show
编译器抱怨:
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
如何创建像 Value
这样的求和类型,其中一个构造函数采用 HashMap Text Text
?
通过使用括号消除 VMap
中 HashMap
的参数歧义:
data Value =
VText Text
| VList [Text]
| VMap (HashMap Text Text)
deriving Show