简体   繁体   中英

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

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)++")»"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM