带有anykind的kind多态性用例

jhdbpxl9  于 2021-07-14  发布在  Java
关注(0)|答案(0)|浏览(224)

鉴于

trait Int                // proper type
trait List[A]            // 1st-order-kinded type constructor 
trait Functor[F[_]]      // higher-order-kinded type constructor taking type constructor
trait I[H[F[_]]]         // higher-order-kinded type constructor taking higher-order type constructor that takes 1st-order type constructor

我们不能传递与声明的类型参数类型不同的类型参数

scala> def f[F[_[_[_]]]] = 42                                                                                                                                
def f[F[_$1]] => Int

scala> f[I]                                                                                                                                                  
val res5: Int = 42

scala> f[Functor]                                                                                                                                            
1 |f[Functor]
  |  ^
  |  Type argument Functor does not conform to upper bound [_$1[_$2]] =>> Any

但是,我们可以通过 AnyKind ```
scala> def f[A <: AnyKind] = 42
def f[A <: AnyKind] => Int

scala> f[Int]
val res10: Int = 42

scala> f[List]
val res11: Int = 42

scala> f[Functor]
val res12: Int = 42

scala> f[I]
val res13: Int = 42

什么是用例 `AnyKind` ? 它解决了什么实际问题?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题