How do I model inheritance in Haskell?

前端 未结 3 2084
走了就别回头了
走了就别回头了 2021-01-04 19:14

I am attempting to create a game engine that is composed of a few different types:

data Camera = Camera ...
data Light = SpotLight ... | DirectionalLight ...         


        
3条回答
  •  一个人的身影
    2021-01-04 20:01

    I would implement it similar to:

    type Position = (Double, Double, Double)
    type Velocity = (Double, Double, Double)
    
    class PhysicalObject a where
        pos :: a -> Position
        velocity :: a -> Velocity
    
    data Camera = Camera
        { camPos :: Position
        , camVel :: Velocity
        } deriving (Eq, Show)
    
    instance PhysicalObject Camera where
        pos = camPos
        velocity = camVel
    

    Then you can do similarly for each type you define that needs PhysicalObject.

提交回复
热议问题