scala - 使用 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
什么?它解决了什么实际问题?
解决方案
推荐阅读
- nosql - 在 AQL Graph 遍历中包含 Entry 节点
- android - 单击搜索按钮时获取 searchView 文本
- base64 - 如何在java中正确解码这个字符串并输出到控制台?
- c# - 如何查找两个日期之间的总月份以及每个月的天数
- jquery - select2 和 .datetimepicker 不是函数
- karate - 无法在 ES6 格式的 JS 文件中声明多行字符串
- javascript - Xhttp 没有在 firefox add on 中触发请求
- http - How can I skip ssl certificate verification on HTTPS connection using the OpenEdge.Net Libraries?
- vim - Vim profile not logging to file
- angular - ERROR InvalidTokenError: Invalid token specified