I am creating an Asteroids clone and want to create a move function. I thought I could use pattern matching on data types, but of course the type signature then does not conform to the actual method. I want to use different code if the t parameter in the Moving data type in the move function is of data type Bullet and tried this, but that doesn't work. Any ideas besides making a specialised move function (which might be better here, but I still want to know if there are other ways).
So I have Moving Asteroid and Moving Bullet and want to pattern match on the type of either Asteroid or Bullet (or other types which I didn't post here to give a minimum example)
What the move function should do in one sentence: Use wraparound for moving all types of Moving o except Moving Bullet.
Some contextual code:
data Moving s = Moving {
position :: Position,
velocity :: Velocity,
size :: Float,
specifics :: s
}
data Bullet = Bullet {
damage :: Int
}
| DeadBullet
data Asteroid = Asteroid
| DeadAsteroid
move :: Float -> Moving o -> Moving o
move secs (Moving (x, y) v@(vx, vy) s t@(Bullet _)) = Moving (x', y') v s t
where x' = (x + vx * secs)
y' = (y + vy * secs)
move secs (Moving (x, y) v@(vx, vy) s t) = Moving (x', y') v s t
where x' = (x + vx * secs) `mod'` width
y' = (y + vy * secs) `mod'` height
Error:
src\Controller.hs:100:42: error:
* Couldn't match expected type `o' with actual type `Bullet'