【发布时间】:2013-06-28 05:43:26
【问题描述】:
类型可以成为类型类的一部分吗?比如:
class KeyTraits v where
keyType :: *
key :: v -> keyType
data TableRow = { date :: Date, metaData :: String, value :: Int }
instance KeyTraits TableRow where
keyType = Date
key = date
这些“类型级”函数可以在其他地方使用吗?例如:
-- automatically deduce the type for the key, from the value type, using
-- the typeclass
data MyMap v = { getMap :: (KeyTraits v) => Map (keyType) v }
我可能做错了什么,但我基本上希望能够像上面那样定义类型关系(例如,某些值可能已经有可以用作键的数据)。如果这不可能或很难,您能否提出一个更符合习惯的更好设计?
谢谢!
【问题讨论】: