J Fritsch
J Fritsch

Reputation: 3358

Why can data type not be serialized although it is member of the show class?

data Mine = Firstname String
          | Lastname String
          deriving (Show, Serialize)

This does not compile and gives the error

Not in scope: type constructor or class `Serialize'

Why is this not seen as member of the Serialize class although it is member of the Show class. I thought that all members of the Show class should serialize without problems?

Upvotes: 3

Views: 243

Answers (2)

mhwombat
mhwombat

Reputation: 8136

If you do want to automatically derive Serialize for your class, you can do it like this:

{-# LANGUAGE DeriveGeneric #-}

import Data.Serialize (Serialize)
import GHC.Generics (Generic)

data Mine = Firstname String
          | Lastname String
          deriving (Show, Generic)

instance Serialize Mine

Upvotes: 3

asm
asm

Reputation: 8898

That error is saying that the Serialize typeclass is not in scope. You need to import the package that defines the typeclass in order to use it. You probably want:

import Data.Serialize

from the cereal package.

Upvotes: 7

Related Questions