Haskell 是否可以在类型类声明和/或实例化中使用GHC.Stack.HasCallStack?
假设我有一个typeclass:Haskell 是否可以在类型类声明和/或实例化中使用GHC.Stack.HasCallStack?,haskell,Haskell,假设我有一个typeclass: import GHC.Stack class Foo a where foo :: a instance Foo Int where foo = undefined 如何将HasCallStack约束添加到foo值?我试过这样做: class (HasCallStack) => Foo a where foo :: a instance (HasCallStack) => Foo Int where foo = undefined
import GHC.Stack
class Foo a where
foo :: a
instance Foo Int where
foo = undefined
如何将HasCallStack约束添加到foo
值?我试过这样做:
class (HasCallStack) => Foo a where
foo :: a
instance (HasCallStack) => Foo Int where
foo = undefined
我得到一个类型错误,如:
source.hs:10:1: error:
• Illegal implicit parameter ‘?callStack::CallStack’
• In the context: HasCallStack
While checking the super-classes of class ‘Foo’
In the class declaration for ‘Foo’
我还尝试只对类或实例进行约束。我在这两种情况下都犯了类似的错误
这有可能吗?还是不可能为类成员获取调用堆栈?如果能够以某种方式获得调用堆栈,它将帮助我更容易地调试某些东西。您只需要在调用站点
foo
处对堆栈进行调试,因此这将编译,并且我相信它将按预期传播隐式:
class Foo a where
foo :: HasCallStack => a
如果将其从
类中删除,该怎么办?因为类型约束的“head”中根本没有a
。