I have this type definition:
data Operace = Op (Int->Int->Int) String (Int->Int->Int) deriving Show
I want to print this type into the interactive shell (GHCi). All that should be printed is the String
field.
I tried this:
instance Show Operace where show (Op op str inv) = show str
But I still keep getting
No instance for (Show (Int -> Int -> Int)) arising from the 'deriving' clause of a data type declaration Possible fix: add an instance declaration for (Show (Int -> Int -> Int)) or use a standalone 'deriving instance' declaration, so you can specify the instance context yourself When deriving the instance for (Show Operace)
I don't want to add Show
for (Int->Int->Int)
, all I want to print is the string.
Thanks for help!
EDIT:
For future reference, the fixed version is:
data Operace = Op (Int->Int->Int) String (Int->Int->Int) instance Show Operace where show (Op _ str _) = str
The second line, deriving (Eq, Show) , is called the deriving clause; it specifies that we want the compiler to automatically generate instances of the Eq and Show classes for our Pair type. The Haskell Report defines a handful of classes for which instances can be automatically generated.
What's a typeclass in Haskell? A typeclass defines a set of methods that is shared across multiple types. For a type to belong to a typeclass, it needs to implement the methods of that typeclass. These implementations are ad-hoc: methods can have different implementations for different types.
Haskell has three basic ways to declare a new type: The data declaration, which defines new data types. The type declaration for type synonyms, that is, alternative names for existing types. The newtype declaration, which defines new data types equivalent to existing ones.
The Eq typeclass provides an interface for testing for equality. Any type where it makes >sense to test for equality between two values of that type should be a member of the Eq >class. All standard Haskell types except for IO (the type for dealing with input and >output) and functions are a part of the Eq typeclass."
The instance declaration you made is the correct way to go. It seems you forgot to remove that faulty deriving
clause from the original data
declaration.
data Operace = Op (Int->Int->Int) String (Int->Int->Int) instance Show Operace where show (Op op str inv) = show str
You can derive Show
, just import Text.Show.Functions
first.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With