Scala 类型参数括号 [英] Scala type parameter bracket

查看:28
本文介绍了Scala 类型参数括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 trait Foo[T] 意味着 T 是一个参数化类型.但是有时候我可以看到trait Foo[T1,T2],或者trait Foo[T1,T2,R],我在里面找不到任何地方描述多个类型的含义一个类型括号,你能指出我在这种情况下的用法吗?据我推测,Foo[T1,T2] 只是意味着,它定义了两个类型参数,它不必取一个 T1 并返回一个 T2.

I know trait Foo[T] means T is a parametrized type. But some times I can see trait Foo[T1,T2], or trait Foo[T1,T2,R], I cannot find anywhere describe the meaning of multiple types inside a type bracket, could you please point me the usages in this case? From what I speculate, Foo[T1,T2] just means, it defined two type parameters, it doesn't have to be take a T1 and return a T2.

当我今天阅读 playframework 文档时,我再次发现自己对这个问题感到困惑.在文档中,它说:

When I read playframework documentation today, I again found myself confused about this question. In the documentation, it says:

BodyParser[A] 基本上是一个 Iteratee[Array[Byte],A],这意味着它接收字节块(只要网络浏览器上传一些数据)并计算出类型为 A 的值作为结果.

A BodyParser[A] is basically an Iteratee[Array[Byte],A], meaning that it receives chunks of bytes (as long as the web browser uploads some data) and computes a value of type A as result.

这个解释听起来像,类型括号内的第二个类型参数是返回类型.

This explanation sounds like, the second the type parameter inside a type bracket is a return type.

我还记得 trait Function2 [-T1, -T2, +R] extends AnyRef 意味着一个接受 T1T2,返回一个 R.

I also remember that trait Function2 [-T1, -T2, +R] extends AnyRef means a function that takes a T1 and T2, return a R.

为什么他们把返回类型放在括号里?这是否意味着括号中的所有最后一个参数都是返回类型?或者他们只是碰巧为返回类型定义了一个新类型 R?

Why do they put the return type in the bracket? Does it mean all the last parameter in a bracket is a return type? Or they just happened defined a new type R for the return type?

推荐答案

类型括号内的多个类型意味着对多个类型进行类型参数化.举个例子

Multiple types inside a type bracket means type parametrization on multiple types. Take for example

trait Pair[A, B]

这是一对值,一个类型为 A,另一个类型为 B.

This is a pair of values one having type A the other having type B.

更新:

我认为您对类型参数的语义解释得太多了.由多个参数参数化的类型仅此而已.特定类型参数在类型参数列表中的位置没有任何特殊之处.特别是类型参数列表中的最后一个参数不需要代表返回类型".

I think you are interpreting too much into the semantics of type parameters. A type parametrized by multiple parameters is just that and nothing more. The position of a specific type parameter in the list of type parameters does not make it special in any way. Specifically the last parameter in a list of type parameters does not need to stand for 'the return type'.

您引用的播放框架中的句子解释了这一特定类型的类型参数的语义.它不能推广到其他类型.Function 类型也是如此:这里的最后一个类型参数恰好表示返回类型".但这不一定适用于其他类型.上面的 Pair[A, B] 类型就是这样一个例子.这里 B 是该对的第二个组件的类型.这里根本没有返回类型"的概念.

The sentence from the play framework which you quoted explains the semantics of the type parameters for this one specific type. It does not generalize to other types. The same holds for the Function types: here the last type parameter happens to mean 'the return type'. This is not necessarily the case for other types though. The type Pair[A, B] from above is such an example. Here B is the type of the second component of the pair. There is no notion of a 'return type' here at all.

参数化类型的类型参数可以出现在参数化类型定义内的任何地方,其中常规"类型可以出现.也就是说,类型参数只是类型的名称,仅当参数化类型本身被实例化时才绑定到实际类型.

Type parameters of a parametrized type can appear anywhere inside the definition of the parametrized type where a 'regular' type could appear. That is, type parameters are just names for types which are bound to the actual types only when the parametrized type itself is instantiated.

考虑下面对类元组的定义:

Consider the following definition of a class Tuple:

class Tuple[A, B](a: A, b: B)

它被实例化为一个 Int 和 String 元组类型,如下所示:

It is instantiated to a type of a tuple of Int and String like this:

type TupleIntString = Tuple[Int, String]

本质上与

class TupleIntString(a: Int, b: String)     

有关官方来源,请查看 Scala 语言规范.特别是第 3.4 节基本类型和成员定义"下 1. 第 4 个要点说:参数化类型 C[T_1, ..., T_n] 的基本类型是类型 C 的基本类型,其中每次出现 aC 的类型参数 a_i 已被相应的参数类型 T_i 替换."

For an official source check the Scala Language Specification. Specifically Section 3.4 "Base Types and Member Definitions" under 1. the 4th bullet point says: "The base types of a parameterized type C[T_1, ..., T_n] are the base types of type C , where every occurrence of a type parameter a_i of C has been replaced by the corresponding parameter type T_i."

这篇关于Scala 类型参数括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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