scala泛型上边界
In my previous post, I have discussed about Scala Variance in detail. In this post, we are going to discuss about “Scala Type Bounds”.
在上一篇文章中,我详细讨论了Scala Variance 。 在这篇文章中,我们将讨论“标量类型边界”。
In Scala, Type Bounds are restrictions on Type Parameters or Type Variable. By using Type Bounds, we can define the limits of a Type Variable.
在Scala中,类型界限是对类型参数或类型变量的限制。 通过使用类型边界,我们可以定义类型变量的限制。
Scala Type Bounds give us the following benefit:
Scala类型绑定为我们带来以下好处:
Scala supports the following Type Bounds for Type Variables:
Scala支持以下类型变量的类型界限:
We are going to discuss these concepts in detail with examples in next sections.
我们将在下一部分的示例中详细讨论这些概念。
In Scala, we can define Upper Bound on Type Parameter as shown below:
在Scala中,我们可以在“类型参数”上定义上限,如下所示:
Description:-
Here T is a Type Parameter ans S is a type. By declaring Upper Bound like “[T <: S]” means this Type Parameter T must be either same as S or Sub-Type of S.
描述:-
这里T是类型参数,而S是类型。 通过将“上界”声明为“ [T <:S] ”,表示此类型参数T必须与S相同或S的子类型。
Example-1:-
示例1:-
[T <: Ordered[T]]
Here We have defined Upper Bound from Type Parameter T to Type Ordered[T]. Then T much be either Ordered or subtype of Ordered type.
在这里,我们定义了从类型参数T到类型Ordered [T]的上限。 然后,T要么是Ordered类型,要么是Ordered类型的子类型。
Example-2:-
Write a Scala program to demonstrate Scala Upper Bound.
示例2:-
编写一个Scala程序来演示Scala上界。
class Animal
class Dog extends Animal
class Puppy extends Dog
class AnimalCarer{
def display [T <: Dog](t: T){
println(t)
}
}
object ScalaUpperBoundsTest {
def main(args: Array[String]) {
val animal = new Animal
val dog = new Dog
val puppy = new Puppy
val animalCarer = new AnimalCarer
//animalCarer.display(animal)
animalCarer.display(dog)
animalCarer.display(puppy)
}
}
This program works fine with commenting the following line.
该程序在注释以下行时效果很好。
//animalCarer.display(animal)
If we uncomment this line and try to run it, we will get compilation error. Because we have defined Upper Bound as shown below:
如果我们取消注释此行并尝试运行它,则会得到编译错误。 因为我们已经定义了上界,如下所示:
class AnimalCarer{
def display [T <: Dog](t: T){
println(t)
}
}
Here we have defined “[T <: Dog]” that means “display” method accepts only either Dog class object or subclass type (i.e. Puppy) of Dog Class. That's why if we pass Dog Super class, we will get “Type Mismatch” compilation error.
在这里,我们定义了“ [T <:Dog]”,这意味着“显示”方法仅接受Dog类对象或Dog类的子类类型(即Puppy)。 这就是为什么如果我们通过Dog Super类,将会得到“ Type Mismatch”(编译不正确)的错误。
In Scala, we can define Lower Bound on Type Parameter as shown below:
在Scala中,我们可以定义“类型参数下界”,如下所示:
Description:-
Here T is a Type Parameter ans S is a type. By declaring Lower Bound like “[T >: S]” means this Type Parameter T must be either same as S or Super-Type of S.
描述:-
这里T是类型参数,而S是类型。 通过将“下界”声明为“ [T>:S] ”,表示此类型参数T必须与S相同或为S的超类型。
Example-1:-
示例1:-
[T >: Ordered[T]]
Here We have defined Lower Bound from Type Parameter T to Type Ordered[T]. Then T much be either Ordered or supertype of Ordered type.
在这里,我们定义了从类型参数T到类型Ordered [T]的下界。 然后,T要么是有序类型,要么是有序类型的超类型。
Example-2:-
示例2:-
class Animal
class Dog extends Animal
class Puppy extends Animal
class AnimalCarer{
def display [T >: Puppy](t: T){
println(t)
}
}
object ScalaLowerBoundsTest {
def main(args: Array[String]) {
val animal = new Animal
val dog = new Dog
val puppy = new Puppy
val animalCarer = new AnimalCarer
animalCarer.display(animal)
animalCarer.display(puppy)
animalCarer.display(dog)
}
}
Here Dog is not a subtype of Puppy, but still this program works fine because Dog is a subtype of Animal and we have defined “Lower Bound” on Type Parameter T as shown below:
这里Dog不是Puppy的子类型,但是此程序仍然可以正常工作,因为Dog是Animal的子类型,我们在类型参数T上定义了“下界”,如下所示:
class AnimalCarer{
def display [T >: Puppy](t: T){
println(t)
}
}
If we remove Lower Bound definition in this class, then we will get some compilation errors.
如果我们在此类中删除Lower Bound定义,则将得到一些编译错误。
In Scala, View Bound is used when we want to use existing Implicit Conversions automatically. We can define View Bound on Type Parameter as shown below:
在Scala中,当我们要自动使用现有的隐式转换时,将使用“查看范围”。 我们可以在“类型参数”上定义“视图绑定”,如下所示:
Description:-
In some scenarios, we need to use some Implicit conversions automatically to solve our problem statement. We can use Scala’s View Bounds to utilize these Implicits.
描述:-
在某些情况下,我们需要自动使用一些隐式转换来解决我们的问题陈述。 我们可以使用Scala的View Bounds来利用这些隐含特性。
Example:-
Write a Scala program to compare Strings with Relational operators(like Int’s 10 > 12).
例:-
编写一个Scala程序,将字符串与关系运算符进行比较(例如Int的10> 12)。
class Person[T <% Ordered[T]](val firstName: T, val lastName: T) {
def greater = if (firstName > lastName) firstName else lastName
}
object ScalaViewBoundsTest {
def main(args: Array[String]) {
val p1 = new Person("Rams","Posa")
val p2 = new Person("Chintu","Charan")
println(p1.greater)
println(p2.greater)
}
}
Output:-
输出:-
Rams
Chintu
If we don’t use Scala’s View Bound operator “<%”, then we will get the following error message.
如果我们不使用Scala的View Bound运算符“ <%”,则将收到以下错误消息。
error: value > is not a member of type parameter T
That’s it all about Scala Upper Bounds, Lower Bounds and View Bounds. We will discuss some more Scala concepts in my coming posts.
这就是Scala上界,下界和视图界的全部内容。 我们将在我的后续文章中讨论更多Scala概念。
Please drop me a comment if you like my post or have any issues/suggestions.
如果您喜欢我的帖子或有任何问题/建议,请给我评论。
翻译自: https://www.journaldev.com/9609/scala-typebounds-upper-lower-and-view-bounds
scala泛型上边界