Dmy*_*nko 4 stack-overflow haskell typeclass ghci
为什么在GHCI(版本7.6.2)中尝试执行此操作会导致堆栈溢出?如何在GHCI会话期间派生类型类实例或为什么这不可能?
*Main> data T = T Int
*Main> let t = T 42
*Main> instance Show T
*Main> t
*** Exception: stack overflow
Run Code Online (Sandbox Code Playgroud)
我知道我可以deriving Show在类型声明中使用,但这个技巧对于检查从文件加载的类型很有用.
Dan*_*her 10
您需要实现至少一个show或showsPrec实例才能工作.在类中,有show使用showsPrec(via shows)和showsPrec使用的默认实现show:
showsPrec _ x s = show x ++ s
show x = shows x ""
Run Code Online (Sandbox Code Playgroud)
和
shows = showsPrec 0
Run Code Online (Sandbox Code Playgroud)
所以
instance Show T
Run Code Online (Sandbox Code Playgroud)
创建一个循环实例.拨打show电话showsPrec,拨打电话show,......
通过StandaloneDeriving语言扩展,您可以
ghci> :set -XStandaloneDeriving
ghci> deriving instance Show T
Run Code Online (Sandbox Code Playgroud)
在提示符处派生实例.