为什么这段代码不起作用?
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 扩展
最佳答案
你需要ScopedTypeVariables
和forall
介绍get a scoped type variable:
{-# LANGUAGE ScopedTypeVariables #-}
bar :: forall a. Foo a => a -> Int
bar _ = foo (Proxy :: Proxy a)
关于haskell - 如何正确使用代理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30795914/