Why do I get stack overflow trying to do this in GHCI (version 7.6.2)? How can I derive a typeclass instance during a GHCI session or why is this not possible?
*Main> data T = T Int
*Main> let t = T 42
*Main> instance Show T
*Main> t
*** Exception: stack overflow
I know I can use deriving Show
at the type declaration, but this trick would be useful for inspecting types loaded from files.
You need to implement at least one of show
or showsPrec
for the instance to work. In the class, there are default implementations of show
using showsPrec
(via shows
), and of showsPrec
using show
:
showsPrec _ x s = show x ++ s
show x = shows x ""
and
shows = showsPrec 0
so
instance Show T
creates a looping instance. Calling show
calls showsPrec
, which calls show
, which ...
With the StandaloneDeriving
language extension, you can
ghci> :set -XStandaloneDeriving
ghci> deriving instance Show T
derive the instance at the prompt.
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