微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AnyKind 的种类多态性用例

如何解决AnyKind 的种类多态性用例

给定

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 的用例是什么?它解决了什么实际问题?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。