我想用haskell来实现一个游戏,并且想使用一个类型类的系统来实现这个item系统。它的工作是这样的:Haskell Typeclass检查
data Wood = Wood Int
instance Item Wood where
image a = "wood.png"
displayName a = "Wood"
instance Flammable Wood where
burn (Wood health) | health' <= 0 = Ash
| otherwise = Wood health'
where health' = health - 100
在项目和易燃类是这样的:
class Item a where
image :: a -> String
displayName :: a -> String
class Flammable a where
burn :: (Item b) => a -> b
要做到这一点,我需要一种方法来检测值是否是一个实例一个类型类。
Data.Data模块提供了类似的功能,这使我相信这是可能的。
我不确定你在做什么符合Haskell类型模型。值是一个类型的实例应该是静态可证明的。 – millimoose 2012-02-05 16:13:36
值不能是类型类的实例。类型是类型类的实例。 – 2012-02-05 16:23:37
请参见[本FAQ文章](http://www.haskell.org/haskellwiki/FAQ#I.27m_making_an_RPG._Should_I_define_a_type_for_each_kind_of_monster.2C_and_a_type_class_for_them.3F)。 – ehird 2012-02-05 16:35:47