如何正确使用代理?

时间:2015-06-12 05:34:47

标签: haskell

为什么这段代码不起作用?

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扩展

1 个答案:

答案 0 :(得分:11)

get a scoped type variable:

需要ScopedTypeVariables forall简介
{-# LANGUAGE ScopedTypeVariables #-}

bar :: forall a. Foo a => a -> Int
bar _ = foo (Proxy :: Proxy a)