为什么 Set 不实现 PartialFunction? [英] Why does not Set implement PartialFunction?

查看:50
本文介绍了为什么 Set 不实现 PartialFunction?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Scala 编程:综合分步指南,第 2 版,第 546 页:

Programming in Scala: A Comprehensive Step-by-Step Guide, 2nd Edition, page 546:

在 Iterable 下面的继承层次结构中,您可以找到三个特征:Seq、设置和映射.这三个特征的一个共同点是它们都使用其 apply 和 isDefinedAt 实现 PartialFunction 特性方法.

In the inheritance hierarchy below Iterable you find three traits: Seq, Set, and Map. A common aspect of these three traits is that they all implement the PartialFunction trait with its apply and isDefinedAt methods.

然而,这段代码无法编译(2.8.2 和 2.10.2 都试过了):

However, this code does not compile (tried both 2.8.2 and 2.10.2):

Set(1, 2, 3).isDefinedAt(1)

有错误:

value isDefinedAt is not a member of scala.collection.immutable.Set[Int]

这是书中的错误吗?

推荐答案

看起来确实如此.深入到 Scala 2.5.0:

Sure looks like it. Going as far as Scala 2.5.0:

  • SeqMap 扩展 PartialFunction:
  • Set 没有.

这似乎很合理:Set 从对象映射到 Boolean 值,所以它总是定义(true 用于成员,false 表示非成员).

It seems sensible: Set maps from objects to Boolean values, so it's always defined (true for members, false for nonmbers).

这篇关于为什么 Set 不实现 PartialFunction?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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