Maa*_*mon 3 generics scala wildcard existential-type higher-kinded-types
我想知道为什么以下有效(注意 Functor 或 Applicative 与示例无关)
trait Functor[F[_]]
trait Applicative[F[_]] extends Functor[F]
Run Code Online (Sandbox Code Playgroud)
但不是
trait Functor[F[_]]
trait Applicative[F[_]] extends Functor[F[_]]
Run Code Online (Sandbox Code Playgroud)
我的问题是F vs F[_]
这里的规则是什么?
特别是编译器给出了一个严重的错误:
F[_] 不接受类型参数,预期:1
下划线 ( _) 在定义站点 (1) 和调用站点 (2) 中具有不同的含义。
(1) 在定义站点下划线表示泛型(类型参数)是类型构造函数(* => *)而不是正确类型(*)。
trait A[X] // X is a proper type, *
// ^^^ definition
trait B[X] extends A[X] // X is a proper type, *
// ^^^ definition, ^^^ call
trait A[F[_]] // F is a type constructor, * => *
// ^^^^ definition
trait B[F[_]] extends A[F] // F is a type constructor, * => *
// ^^^^ definition, ^^^ call
Run Code Online (Sandbox Code Playgroud)
因为在定义站点它已经被强调过一次它F是一个类型构造函数,在调用站点它总是被称为 just F(_如果你的意思是相同的类型构造函数,则不需要额外F)。
(2) 在调用点,对于类型构造函数 ( * => *) F,F[_]表示存在类型。存在类型是适当的类型 ( *)。
trait A[X] // X is a proper type, *
// ^^^ definition
trait B[F[_]] // F is a type constructor, * => *
extends A[F[_]] // F[_] is an existential type, *
// ^^^^ definition, ^^^^ call
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
146 次 |
| 最近记录: |