【发布时间】:2020-10-07 21:35:51
【问题描述】:
考虑以下类型参数子句[F[_] <: Int] in
def h[F[_] <: Int] = ???
类型构造函数F 由正确的类型Int 界定。现在h[List] 和h[Int] 都是非法的
scala> def h[F[_] <: Int] = ???
|
def h[F[_] <: Int]: Nothing
scala> h[List]
^
error: type arguments [List] do not conform to method h's type parameter bounds [F[_] <: Int]
scala> h[Int]
^
error: Int takes no type parameters, expected: 1
那么为什么[F[_] <: Int] 合法?
【问题讨论】:
-
你可以这样做:
type Test[A] = 3(或任何其他 Int,或 Int 本身) 然后你可以拨打h[Test]。 -
您可以将
F[_ <: A] <: B视为f: A => B的类型级模拟。
标签: scala generics type-constraints higher-kinded-types type-constructor