変換もclassで

上記で型の変換が多い場合は関数を使う。そして変換型が多くなって関数名がながくなるならclassにしてしまうのがいいかも。

class CastToken t where
  castTo :: Token -> t
  castBack :: t -> Token

instance CastToken SB.Bullet where
  castTo (Bullet {x = ax, y = ay}) = SB.Bullet {SB.x = ax, SB.y = ay}
  castBack (SB.Bullet {SB.x = ax, SB.y = ay}) = Bullet {x = ax, y = ay}
  
instance CastToken SE.Enemy where
  castTo (Enemy {x = ax, y = ay}) = SE.Enemy {SE.x = ax, SE.y = ay}
  castBack (SE.Enemy {SE.x = ax, SE.y = ay}) = Bullet {x = ax, y = ay}

instance TokenT Token where
  update b@(Bullet {}) 
    = castBack $ update ((castTo b) :: SB.Bullet)
  update e@(Enemy {}) 
    = castBack $ update ((castTo e) :: SE.Enemy)