scala - Haskell 和 Scala 中的通用量化类型?

标签 scala haskell

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 的不同方式:

  1. 启用 ScopedTypeVariables:不是必需的,因为 Scala 类型变量默认是作用域的。

  2. 对于存在类型:Scala 直接支持它们。

  3. 对于更高级别的类型,即具有嵌套在函数箭头下的 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/

相关文章:

scala - Play 2.0 集成 TDD/BDD 数据库模型生成

scala - 在 Vim 中折叠 Scala 导入

haskell - Data.Dequeue 和 BankersDequeue

haskell - 无法使用 IO Monad 打印到文件

scala - 当在 FunSuite 的测试函数之外使用 SharedSparkContext (sc) 时,是什么导致了 NullPointerException?

.net - 如何在 Windows Phone 上运行 Scala?

Scala - 提取器未应用混淆

haskell - 如何解决 RandomGen 的 "rigid type variable bound by"错误?

haskell - Haskell 中的 main 函数是否总是以 main = do 开头?

haskell - 使用 GHC API 时查找 cabal 包