这是 Scala 中更高级的类型吗? [英] Is this a higher kinded type in Scala?

查看:29
本文介绍了这是 Scala 中更高级的类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有以下定义

type MyMap = Map[String, List[Map[Int, String]]] 

Map 可以定义为更高级的类型吗?

Can Map be defined as a higher kinded type ?

推荐答案

它不应该.

您可以用值和函数进行类比.您有一些基本值,它们不是函数,例如 5"foo".然后是简单的函数,它接受简单的值作为参数并返回简单的值,例如 +length.高阶函数是具有其他函数作为参数或结果的函数.例如,takeWhilemapfoldLeft 是高阶函数.

You can make an analogy with values and functions. You have basic values, which are not functions, such as 5 and "foo". You have then simple functions, which takes simple values as arguments and return simple values, such as + or length. Higher order functions are functions which have other functions as parameters or result. For instance takeWhile, map, or foldLeft are higher order functions.

如果考虑类型,有简单类型,它们是值的实际类型,例如 IntString,甚至 Int =>String 和 List[Double](现在我考虑每个值,无论简单与否,包括函数).然后是参数类型,它们也可以称为类型构造函数(将它们称为类型函数会使类比更清晰).List(没有实例化泛型参数)并不是真正的值类型,您不能将 val 声明为 List 类型,它必须是 List[Something].所以 List 可以被认为是一个函数,它给定一个简单类型(比如 Int)返回另一个简单类型(List[Int]).IntStringDouble 和 Int => String 据说具有 kind *,而 List 有种类 * ->*.ListMap 等参数类型类似于简单函数.

If you consider types, there are simple types, which are the actual types of values , such as Int, String, or even Int => String and List[Double] (now I consider every values, simple or not, including functions). Then there are parameteric types, which may also be called type constructors (calling them type functions would make the analogy clearer). List (without instanciating the generic parameter) is not really a type for values, you cannot declare a val to be just of type List, it has to be List[Something]. So List may be considered as a function that given a simple type (say Int) returns another simple type (List[Int]). Int, String, and Double and Int => String are said to have kind *, while List has kind * -> *. Parametric types such as List or Map are the analogous of simple functions.

正如高阶函数是具有函数(而不是简单值)参数的函数一样,高阶类型(或有时是更高级的)是具有类型构造函数参数的类型,而不仅仅是简单的类型参数.它有种类 (* -> *) ->*,或者更复杂的东西.它们用 HigherOrder[C[_]]HigherOrder[C[X]] 声明,以告诉类型参数 C, 本身是一个参数类型或类型构造函数.请注意,这必须出现在类型声明中,而不是类型实例化中.List 被声明为特征 List[A],所以它是参数化的,但不是更高阶的.如果你用 List[Seq[Map[Int, Set[Double]]] 实例化一个,那不会使 List 的顺序更高.高阶类型将接受 List(而不是 List[Int])作为其参数,可以声明 val x : HigherOrder[List] = ....

Just as a higher order function is a function with function (instead of simple value) parameter, a Higher order type (or sometimes higher kinded) is a type that has a type constructor parameter, rather than just simple type parameters. It has kind (* -> *) -> *, or something more complex. They are declared with HigherOrder[C[_]], or HigherOrder[C[X]], to tell that the type parameter, C, is itself a parametric type, or type constructor. Note that this has to appear in the type declaration, not the type instantiation. List is declared trait List[A], so it is parametric, but not higher order. If you instanciate one with List[Seq[Map[Int, Set[Double]]], that won't make List's order any higher. A higher order type would accept List (rather than List[Int]) as its parameter, one could declare val x : HigherOrder[List] = ....

高阶类型在库中不太常见,你可以在集合库的血腥细节中发现一些,例如 GenericCompanion.你可以在 scalaz 中找到很多.

Higher order types are not too common in the library, you can spot a few in the gory details of the collection library, such as GenericCompanion. You can find a lot of them in scalaz.

这篇关于这是 Scala 中更高级的类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆