Reputation: 4993
Let's say I have this typeclass:
import GHC.Stack
class Foo a where
foo :: a
instance Foo Int where
foo = undefined
How can I add the HasCallStack constraint to the foo
value? I've tried like this:
class (HasCallStack) => Foo a where
foo :: a
instance (HasCallStack) => Foo Int where
foo = undefined
And I get a type error like:
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’
I've also tried only having the constraints on either the class or the instance. I got similar errors in both cases.
Is this possible somehow? Or is it impossible to get call stacks for class members? It would help me debug something quite a bit more easily at the moment if it was possible somehow to get the call stack.
Upvotes: 6
Views: 250
Reputation: 3071
You only need a the stack at the call site of foo
, so this compiles, and I believe it will propagate the implicit as expected:
class Foo a where
foo :: HasCallStack => a
Upvotes: 7