简体   繁体   English

Haskell继承,数据,构造函数

[英]Haskell inheritance, data, constructors

So I want to define multiple data classes for my Asteroids game/assignment: 所以我想为我的Asteroids游戏/作业定义多个数据类:

data One = One {oneVelocity :: Velocity, onePosition :: Position, (((other properties unique to One)))}
data Two = Two {twoVelocity :: Velocity, twoPosition :: Position, (((other properties unique to Two)))}
data Three = Three {threeVelocity :: Velocity, threePosition :: Position, (((other properties unique to Three)))}

As you can see I have multiple data classes with some overlapping properties (velocity, position). 如您所见,我有多个具有重叠属性(速度,位置)的数据类。 That also meant that I had to give them different names per data class ("oneVelocity", "twoVelocity", ...). 这也意味着我必须为每个数据类赋予不同的名称(“oneVelocity”,“twoVelocity”,......)。

Is there a way I can let these types of data extend something? 有没有办法让这些类型的数据扩展? I thought of using one datatype with multiple constructors, but some of these current data classes are very different and I don't thing they should reside in one data class with multiple constructors. 我想过使用一个带有多个构造函数的数据类型,但是这些当前数据类中的一些是非常不同的,我不应该将它们存放在一个具有多个构造函数的数据类中。

You should probably use just a single data type for all of these, but parameterised on the specific details: 您应该只为所有这些使用单一数据类型,但参数化具体细节:

data MovingObj s = MovingObj
        { velocity :: Velocity
        , position :: Position
        , specifics :: s }

Then you can create eg asteroid :: MovingObj AsteroidSpecifics , but you can also write functions that work with any such moving object like 然后你可以创建例如asteroid :: MovingObj AsteroidSpecifics ,但你也可以编写适用于任何这样的移动对象的函数

advance :: TimeStep -> MovingObj s -> MovingObj s
advance h (MovingObj v p s) = MovingObj v (p .+^ h*^v) s

There is no inheritance in Haskell (at least, not the kind you associate with object-oriented classes). Haskell中没有继承(至少,不是与面向对象类相关联的那种)。 You just want composition of data types. 您只需要组合数据类型。

data Particle = Particle { velocity :: Velocity
                         , position :: Position 
                         }

-- Exercise for the reader: research the GHC extension that
-- allows all three of these types to use the same name `p`
-- for the particle field.
data One = One { p1 :: Particle
               , ... }
data Two = Two { p2 :: Particle
               , ... }
data Three = Three { p3 :: Particle
                   , ... }

Or, you can define a type that encapsulates the other properties, and let those be added to different kinds of Particle s. 或者,您可以定义封装其他属性的类型,并将这些属性添加到不同类型的Particle

data Properties = One { ... } 
                | Two { ... }
                | Three { ... }

data Particle = Particle { velocity :: Velocity
                         , position :: Position
                         , properties :: Properties
                         } 

(Or see @leftaroundabout's answer , which is a nicer way of handling this approach.) (或者参见@ leftaroundabout的答案 ,这是处理这种方法的一种更好的方法。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM