相关疑难解决方法(0)

具有记录和类类型的 Haskell 多态函数

这个帖子是这个帖子的后续。

我正在将一个简单的战斗系统实现为玩具项目,这是您可以在《最终幻想》等游戏中找到的典型系统。我已经用类类型 + 自定义实例解决了臭名昭著的“命名空间污染”问题。例如:

type HitPoints = Integer
type ManaPoints = Integer

data Status = Sleep | Poison | .. --Omitted
data Element = Fire | ... --Omitted

class Targetable a where
    name :: a -> String
    level :: a -> Int
    hp :: a -> HitPoints
    mp :: a -> ManaPoints
    status :: a -> Maybe [Status]

data Monster = Monster{monsterName :: String,
                       monsterLevel :: Int,
                       monsterHp :: HitPoints,
                       monsterMp :: ManaPoints,
                       monsterElemType :: Maybe Element,
                       monsterStatus :: …
Run Code Online (Sandbox Code Playgroud)

polymorphism haskell records

5
推荐指数
1
解决办法
1262
查看次数

标签 统计

haskell ×1

polymorphism ×1

records ×1