理解 Scala 中的类型参数 [英] Understanding type Parameters in Scala

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

问题描述

我正在尝试理解 Scala 中的类型参数.让我们看下面的一般示例:

def func1[T](a : T) : T = a

我知道 func1 接受任何类型的 1 个参数,并返回完全相同类型的参数.我不明白的是为什么:

def func1[T]

为什么 [T] 紧跟在 function1 之后??我们可以简单地在 func1 之后不加 [T] 来编写它,例如:

def func1(a : T) : T = a

1) func1 后面的 [T] 是什么意思,为什么我们把它放在那里?

2) 为什么我们对类做同样的事情?

class MyClass[T]{...}

我的意思是 MyClass 实例是 MyClass 类型.[T] 在那里是什么意思?你不是说我有一个 MyClass 类型的布尔类,你说我有一个 MyClass 类型的对象对吗?

提前致谢.

解决方案

  1. func1 后面的 [T] 是什么意思,为什么要把它放在那里?

func[T]中的[T]定义了一个类型参数T.你的函数可以像 func[String]("Hello") 一样被调用,其中 String 被替换为 T.您也可以像 func("Hello") 一样调用它,因为 Scala 编译器足够聪明,可以推断 T 必须是 String.

那为什么我们在定义的时候一定要写func[T]呢?我们需要区分由类型参数给出的类型参数和由实际类型给出的参数.如果你这样写:def func1(a : T) : T = a,那么 T 必须是一个实际类型.例如:

T 类def func1(a : T) : T = a//<-- 现在编译

  1. 为什么我们要对类做同样的事情?

您经常希望在类中包含某种类型的对象.如果您在类级别定义类型参数,则该类型将在整个类中保持不变.考虑这个例子:

class Container[T](val t: T) {def isValueEqual(obj: T): Boolean = t.equals(obj)}

这里,obj:T中的TContainer[T]中定义的T类型相同代码>.现在考虑这个例子:

class Container[T](val t: T) {def isValueEqual[T](obj: T): Boolean = t.equals(obj)}

请注意,我也在方法级别定义了一个新的类型参数 (isValueEqual[T]).在这种情况下,方法中定义的 T 将隐藏在类级别定义的 T.这意味着它们可能不是同一类型!你可以这样称呼它:

val c = new Container("Hello")println(c.isValueEqual(5))//5 不是 String 类型!

I am trying to understand the type parameters in Scala. Let's look the following general example:

def func1[T](a : T) : T = a

I understand that func1 takes 1 parameter of any type, and returns that parameter of the very same type. What i don't understand is why:

def func1[T]

Why [T] right after function1?? We could simply write it without [T] after func1, like:

def func1(a : T) : T = a

1) What does that [T] means after func1 and why we put it there?

2) Why we do the same with classes?

class MyClass[T]{...}

I mean MyClass instantiations are of type MyClass. What does [T] means there? You don't say i have a boolean Class of type MyClass, you say i have an object of type MyClass right?

Thanks in advance.

解决方案

  1. What does [T] mean after func1, and why do we put it there?

The [T] in func[T] defines a type parameter T. Your function can be called like func[String]("Hello"), in which String is replaced with T. You can also call it like func("Hello") because the Scala compiler is smart enough to infer that T must be String.

So why do we have to write func[T] when we define it? We need the distinction between arguments of a type given by a type parameter, and arguments given by an actual type. If you write it this: def func1(a : T) : T = a, then T has to be an actual type. For example:

class T

def func1(a : T) : T = a  // <-- This compiles now

  1. Why do we do the same with classes?

You often want to contain an object of some type inside a class. If you define the type parameter at the class level, the type will remain the same throughout your class. Consider this example:

class Container[T](val t: T) {
    def isValueEqual(obj: T): Boolean = t.equals(obj)
}

Here, the T in obj: T is the same type as the T defined in Container[T]. Now consider this example:

class Container[T](val t: T) {
    def isValueEqual[T](obj: T): Boolean = t.equals(obj)
}

Notice that I defined a new type parameter at the method level as well (isValueEqual[T]). In this case, the T defined in the method will shadow the T defined on the class level. This means that they might not be the same type! You could call it like this:

val c = new Container("Hello")
println(c.isValueEqual(5)) // 5 is not of type String!

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

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