Scala类型 10 :交集类型(intersection type)与联合类型(union type)
2015-02-28 18:36
537 查看
</pre><p>交集类型(<em>intersection type</em>):</p><p>Scala中,复合类型(compound type)既交集类型。表达形式:</p><p><pre name="code" class="java">X with Y with Z...
联合类型(union type):
联合类型的表现形式应该为:
X or Y or Z...
在Scala中没有语法形式的支持union type;但可以通过一些技巧实现。
StackOverflow上有两种实现方式。
通过隐式转换(上下文界定):
class StringOrInt[T] object StringOrInt { implicit object IntWitness extends StringOrInt[Int] implicit object StringWitness extends StringOrInt[String] } object Bar { def foo[T: StringOrInt](x: T) = x match { case _: String => println("str") case _: Int => println("int") } }Scala的类型推断也有其影子,可阅读源码。
Currying方式:coming soon...
相关文章推荐
- scala类型系统:10) 交集类型与联合类型
- 联合类型(union type )
- 2.4.2 差别联合类型(discriminated union type)
- Solr-----10、Solr内置fieldType类型
- Scala新手指南中文版 -第五篇 The Option Type(Option类型)
- 第二十五章补充内容 12联合数据类型union
- C语言数据类型:联合(union)
- 谈谈联合类型union的使用
- c语言联合union 的联合变量,联合标记,联合类型定义的区别
- Google Maps API 2.0解析(10-GMapType GTileLayer _GoogleMapMercSpec _KeyholeMapMercSpec地图类型)
- C++:联合类型(union)
- C语言数据类型:联合(union)
- 学点 C 语言(28): 数据类型 - 联合(union)
- C语言的联合(union)类型(27)
- Scala 抽象类型Type
- Scala类型 8:type 关键字
- union联合 数据类型 详细说明
- C语言数据类型:联合(union)
- C语言数据类型:联合(union)
- 联合(union)类型有哪些使用场景