haskell - 如何正确使用代理?

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

最佳答案

你需要ScopedTypeVariablesforall介绍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/

相关文章:

haskell - 函数中的非穷尽模式-Haskell

haskell - 为什么这两段代码的行为不同?

networking - 在 Haskell 中列出网络接口(interface)

list - 绑定(bind) `len = length xs` 然后计算 `len` 会导致 GHC 消耗大量 RAM

string - Haskell 中如何比较字符串?

haskell - 为什么我可以使用这个 "private"值构造函数?

haskell - liftM 可以与 liftA 不同吗?

syntax - Haskell 中树的右旋转 : how is it work?

haskell - 在编写我的 fmap 时遇到问题

multithreading - cancel 和 uninterruptibleCancel 之间的区别(来自 Async 库)