Existentially quantified types解释:
any use of a lowercase type implicitly begins with a
forall
keyword, so the two type declarations for map are equivalent, as are the declarations below:
id :: a -> a
id :: forall a . a -> a
鉴于 Scala 的 scala.Predef#identity ,是否有 forall
等效项,即按照 Haskell 的上述第二个函数?
最佳答案
Scala 中没有 explicit forall
,但它等效于 Haskell 中使用显式 forall
的不同方式:
启用
ScopedTypeVariables
:不是必需的,因为 Scala 类型变量默认是作用域的。对于存在类型:Scala 直接支持它们。
对于更高级别的类型,即具有嵌套在函数箭头下的
forall
的类型:考虑以下示例:f2 :: (forall a. a->a) -> Int -> Int f2 f x = f x
在Scala中
[A](A => A)
不能直接使用,但是trait Poly1 { def apply[A](x: A): A }
等价于它,所以可以实现
f2
:def f2(p: Poly1, x: Int) = p(x)
关于scala - Haskell 和 Scala 中的通用量化类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37536290/