目前我有这样的事情:
data MyData = SomeData | MoreData | EvenMore
data DataLists = DataLists [MyData] [MyData]
这让我显然可以做这样的事情:
myDataList :: DataLists
myDataList = DataLists [SomeData, MoreData] [EvenMore, SomeData]
但我想做这样的事情:
-- Static Module File:
class MyClass b where
evaluate :: b -> Int
data SomeData = SomeData
data MoreData = MoreData
data EvenMore = EvenMore
instance MyClass SomeData where
evaluate _ = 2
instance MyClass MoreData where
evaluate _ = 3
instance MyClass EvenMore where
evaluate _ = 4
data DataList = DataList [MyClass] [MyClass] -- Obviously this is wrong
我希望我的那部分代码保持静态。因为稍后我想使用该模块添加一些类型以放入数据列表中。我需要这个单独的,因为整个模块将用于 2 个不同的项目,我不想继续编辑它。
data MyVeryOwnData = MyVeryOwnData
--I should be able to make up as many datastructures as I want
instance MyClass MyVeryOwnData where
evaluate _ = 99
myList :: DataList
myList = DataList [MyVeryOwnData, SomeData] [SomeData, EvenMore]
我意识到我可以像这样简单地将 MyVeryOwnData 放在模块文件中:
data MyData = SomeData | MyVeryOwnData ...
instance MyClass MyData where
evaluate SomeData = 2
evaluate MoreData = 3
... etc.
但我不想这样做,因为正如我之前所说,我想添加任意数量的数据类型。