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 。 在这篇文章中,我们将讨论“标量类型边界”。

Scala中的Type Bound是什么? (What is Type Bound in Scala?)

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类型绑定的优势 (Advantage of Scala Type Bounds)

Scala Type Bounds give us the following benefit:

Scala类型绑定为我们带来以下好处:

  • Type-Safe Application Development.类型安全的应用程序开发。

Scala类型界限 (Scala Type Bounds)

Scala supports the following Type Bounds for Type Variables:

Scala支持以下类型变量的类型界限:

  • Scala Upper BoundsScala上界
  • Scala Lower BoundsScala下界
  • Scala View BoundsScala视图范围

We are going to discuss these concepts in detail with examples in next sections.

我们将在下一部分的示例中详细讨论这些概念。

Scala上界 (Scala Upper Bounds)

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 Dogclass AnimalCarer{def display [T <: Dog](t: T){println(t)}
}object ScalaUpperBoundsTest {def main(args: Array[String]) {val animal = new Animalval dog = new Dogval puppy = new Puppyval 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”(编译不正确)的错误。

Scala下界 (Scala Lower Bounds)

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 Animalclass AnimalCarer{def display [T >: Puppy](t: T){println(t)}
}object ScalaLowerBoundsTest {def main(args: Array[String]) {val animal = new Animalval dog = new Dogval puppy = new Puppyval animalCarer = new AnimalCareranimalCarer.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定义,则将得到一些编译错误。

Scala视图范围 (Scala View Bounds)

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泛型上边界

scala泛型上边界_Scala类型边界:上边界,下边界和视图边界相关推荐

  1. Android ImageView的scaleType(图片比例类型)属性与adjustViewBounds(调整视图边界)属性

    本文转载自[Android ImageView的scaleType(图片比例类型)属性与adjustViewBounds(调整视图边界)属性]并做了排版的修改(http://www.cnblogs.c ...

  2. php 文件上传mime 类型,php 上传的MIME类型

    一下是文件后缀与MIME类型的对照表 ​123 application/vnd.lotus-1-2-3 3gp video/3gpp ​A开头: ​aab application/x-authowar ...

  3. java 泛型 通配符边界和类型形参边界的区别

    通配符只能有一个边界,而类型形参可以有多个边界 通配符可以有上界或者下界,而类型形参没有下界<super> 参考: http://www.angelikalanger.com/Generi ...

  4. 7.scala初识 柯里化、隐式参数、隐式转换、视图边界、上界、下界、协变、逆变

    1.前言: 学过java我们都知道,java中的继承是对类的增强,java中的代理.装饰是对对象方法的增强.而在scala中,隐式转换和隐式参数是Scala中两个非常强大的功能,隐式的对类的方法进行增 ...

  5. NHibernate自定义集合类型(上):基本实现方式

    前天一篇文章中我说NHibernate的集合类型实现有些"尴尬",它无法使用自定义集合类型,设计也有些古怪--不过在许多朋友的指点下,我意识到NHibernate是可以使用自定义集 ...

  6. swift文件服务器,Swift3一行代码将各种类型文件上传到服务器

    由于之前一直在忙项目,很久没有写过一篇像样的文章了,现在手上的项目基本是完成了,正好工作时间偷个懒写两篇文章. 将相机或相册图片上传到服务器 先看看最常见的图片上传,也可以选择跳过,后面有直接的封装方 ...

  7. 服务器修改mime类型,服务器上设置mime类型

    服务器上设置mime类型 内容精选 换一换 资源包括静态语音,TTS放音以及短消息,在您进行流程编排前,需要先将涉及到的资源,包括语音.短信模板添加到系统中,才能继续配置流程. 开发过程中,您有任何问 ...

  8. SAP PM 初级系列之27 – SAP系统怎么知道某种类型的维修工单检验批上的检验类型是14?

    SAP PM 初级系列之27 – SAP系统怎么知道某种类型的维修工单检验批上的检验类型是14? 比如在SAP系统中,Calibration类型的维修工单,工单下达后自动触发了检验批,检验类型是14. ...

  9. Linux系统上的文件类型

    Linux系统上的文件类型 -: 常规文件 d: directory,目录文件 b: block device,块设备文件,支持以"block"为单位进行随机访问 c: chara ...

最新文章

  1. usaco wormhole(看了官方视频题解)
  2. 求3*4数组的全部元素之和
  3. 驾驭白夜场景、刷新多个SOTA,高效提升多目标追踪与分割
  4. 北邮校园网自动登录 python
  5. SharePoint 2010 工作流解决方案:将 SharePoint Designer 可重用工作流导入 Visual Studio...
  6. springboot+jpa+mysql+redis+swagger整合步骤
  7. 《论道HTML5》内容技术分享活动
  8. 大公司和小公司的比较
  9. resnet,inception,densenet,senet
  10. Demo:第四章:Gateway网关
  11. MySQL复制之gtid_purged与gtid_executed
  12. JVM 垃圾收集器Serial、Parallel Scavenge、ParNew、CMS、G1
  13. linux挂载的硬盘为ro,remount成rw出错问题解决
  14. R语言数据读取以及数据保存
  15. Vim内同时对多行增加或删除相同的内容
  16. go time.after
  17. elasticsearch 7.9.3知识归纳整理(一)之 es,kibana,ik的下载安装
  18. 超快捷还原达梦数据库dmp
  19. ActivePerl从源码安装模块
  20. CES Asia专题|栩栩如生!Artec3D现场展示其3D扫描技术

热门文章

  1. VisualSVN https 钩子失效 关闭服务器信任
  2. C#中自己动手创建一个Web Server(非Socket实现)
  3. 检查用户是否有访问权限
  4. [转载] Python中的enumerate函数介绍
  5. [转载] Python:Numpy详解
  6. SDR与DDR的区别
  7. 匿名函数lambda
  8. Discuz常见小问题-如何关闭验证码
  9. 在数组中寻找出现次数超过数组长度一半的数
  10. NodeJS服务器退出:完成任务,优雅退出