>From Real World Haskell:
data JValue = JString String
| JNumber Double
| JBool Bool
| JNull
| JObject [(String, JValue)]
| JArray [JValue]
deriving (Eq, Ord, Show)
....
type JSONError = String
class JSON a where
toJValue :: a -> JValue
fromJValue :: JValue -> Either JSONError a
instance JSON JValue where
toJValue = id -- Really ?
fromJValue = Right
Now, instead of applying a constructor likeJNumber
to a value to wrap it, we apply thetoJValue
function. If we change a value's type, the compiler will choose a suitable implementation of toJValue to use with it.