是否可以在类型声明和/或实例化中使用GHC.Stack.HasCallStack?

时间:2018-07-25 13:17:34

标签: haskell

比方说我有这个类型类:

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’

我还尝试过仅对类或实例进行约束。在这两种情况下,我都遇到类似的错误。

这有可能吗?还是不可能为类成员获得调用堆栈?如果有可能以某种方式获取调用堆栈,这将帮助我现在更轻松地调试某些东西。

1 个答案:

答案 0 :(得分:5)

您只需要在foo的调用站点上放置一个堆栈,即可进行编译,并且我相信它将按预期传播隐式内容:

class Foo a where
    foo :: HasCallStack => a