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.
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
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