为什么这段代码不起作用?
class Foo a where
foo :: Proxy a -> Int
bar :: Foo a => a -> Int
bar _ = foo (Proxy :: Proxy a)
无法使用以下消息进行编译:
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)
我尝试使用和不使用ScopedTypeVariables扩展
答案 0 :(得分:11)
ScopedTypeVariables
和 forall
简介
{-# LANGUAGE ScopedTypeVariables #-}
bar :: forall a. Foo a => a -> Int
bar _ = foo (Proxy :: Proxy a)