Why this code doesn't work?
class Foo a where
foo :: Proxy a -> Int
bar :: Foo a => a -> Int
bar _ = foo (Proxy :: Proxy a)
It fails to compile with the message:
Could not deduce (Foo a0) arising from a use of `foo'
from the context (Foo a)
bound by the type signature for bar :: Foo a => a -> Int
The type variable `a0' is ambiguous
In the expression: foo (Proxy :: Proxy a)
In an equation for `bar': bar _ = foo (Proxy :: Proxy a)
I tried it with and without ScopedTypeVariables extension
You need ScopedTypeVariables
and forall
introduction to get a scoped type variable:
{-# LANGUAGE ScopedTypeVariables #-}
bar :: forall a. Foo a => a -> Int
bar _ = foo (Proxy :: Proxy 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