scala sealed trait
来源:互联网 发布:支持spi的读卡器淘宝 编辑:程序博客网 时间:2024/05/18 08:15
A sealed
trait can be extended only in the same file as its declaration.
They are often used to provide an alternative to enums
. Since they can be only extended in a single file, the compiler knows every possible subtypes and can reason about it.
For instance with the declaration:
sealed trait Answercase object Yes extends Answercase object No extends Answer
The compiler will emit a warning if a match is not exhaustive:
scala> val x: Answer = Yesx: Answer = Yesscala> x match { | case No => println("No") | }<console>:12: warning: match is not exhaustive!missing combination Yes
So you should use sealed traits (or sealed abstract class) if the number of possible subtypes is finite and known in advance. For more examples you can have a look atlist and option implementations.
0 0
- scala sealed trait
- sealed trait Scala学习笔记-面向对象篇
- Scala:trait
- Scala Trait
- Scala Trait
- Scala-trait
- Scala的sealed关键字
- scala sealed关键字
- scala sealed关键字
- scala中的trait
- Scala Trait 的线性化
- Scala 有趣的Trait
- scala——trait
- Scala的Trait
- Scala trait介绍
- Scala trait 未完待续
- Scala的trait
- scala中的Trait
- 查询数据库重复记录方法
- 两个springboot项目如何部署在同一个服务上,two spring boot applications deploy in same server
- Mysql系列(一) 简介
- 【Unity】计时器
- 仿直播间玩色子游戏,AnimatorSet的综合使用
- scala sealed trait
- error: insufficient permission for adding an object to repository database ./objects
- Json介绍以及C++实现
- php开启openssl
- Java虚拟机学习(10):类加载器(ClassLoader)
- spring ioc 解耦方式的实现
- 什么是WEB?EJB?
- 用文件方式复制内容到另一个文件
- "严重: Null component Catalina:type=JspMonitor,name=jsp,WebModul Unsupported major.minor version 51.0