Reputation: 181
I wanted to write this data type:
data GameState = GameState {
players :: [Player],
lasers :: [(Float, Float, Float)],
asteroids :: [Asteroid],
score :: Int,
width :: Int,
height :: Int,
keys :: S.Set Key,
lastTimeAsteroidAdded :: Int,
screen :: Int,
mouseclicks :: S.Set (Float, Float)
}
deriving (Show, Generic, ToJSON)
to a JSON file. Therefor I needed to let GameState be derived from ToJSON, but now it says I need to create a stand-alone deriving instance for Key. But how do I do that?
The message that I got:
No instance for (ToJSON Key)
arising from the 'deriving' clause of a data type declaration
Possible fix:
use a standalone 'deriving instance' declaration,
so you can specify the instance context yourself
• When deriving the instance for (ToJSON GameState)
Upvotes: 1
Views: 168
Reputation: 31
The solution to your problem is rather simple. Since you have no access to Key (it is defined in Gloss) and Key does not derive Generic, you first have to derive an instance of Generic for Key:
deriving instance Generic Key
This does require the usage of the following extra extension:
{-# LANGUAGE StandaloneDeriving #-}
Now that you have an Generic instance for Key, an instance for ToJSON can be defined:
instance TOJSON Key
Upvotes: 1