marcosh
marcosh

Reputation: 9008

access type variable values in instance declaration

I have a newtype wrapping a function

newtype Operation a b = Operation (a -> b)

I would like to write a Show instance for this datatype providing information like "Operation(Int -> String)". To do this I would need to access the value of the type variables a and b in the implementation of show. Is this possible?

I would say no, but Haskell never stops to amaze me, so I thought I might ask

Upvotes: 0

Views: 53

Answers (1)

leftaroundabout
leftaroundabout

Reputation: 120751

First let me say that this is a bad idea: Show instances should actually give you the contents/value, not just information about the type.

Anyway though...

import Data.Typeable

instance (Typeable a, Typeable b) => Show (Operation a b) where
  show (Operation f) = "«Operation("++show (typeOf f)++")»"

Upvotes: 4

Related Questions