scala.collection.immutable.WrappedString 需要隐式 CanBuildFrom 来实现记录的功能吗?
WrappedString Scaladoc 2.8.1:
“此类充当包装器,使用索引序列中找到的所有操作来扩充字符串。 此类与 StringOps 之间的区别在于,调用诸如 filter 和 map 之类的转换器方法将生成 WrappedString 类型的对象,而不是 String”,
scala> import scala.collection.immutable.WrappedString
import scala.collection.immutable.WrappedString
scala> val s = new WrappedString("foo")
s: scala.collection.immutable.WrappedString = WrappedString(f, o, o)
scala> s.filter(x => true)
res1: scala.collection.immutable.WrappedString = WrappedString(f, o, o)
scala> s.map(x => x)
res2: scala.collection.immutable.IndexedSeq[Char] = Vector(f, o, o)
唉,map 返回的是 Vector 而不是 WrappedString。如果我理解的话这是正确的:
Filter 可以工作,因为它只是使用 newBuilder 方法,但是 map 需要一个隐式的 CanBuildFrom 来处理 WrappedString,就像 BitSet 一样,这是代码或文档中的错误还是我遗漏了一些东西
?对我来说有任何意义:
def map [B] (f: (Char) ⇒ B) : WrappedString[B]
def map [B, That] (f: (Char) ⇒ B)(implicit bf: CanBuildFrom[WrappedString, B, That]) : That
不应该是:
def map [B] (f: (Char) ⇒ Char) : WrappedString
def map [B, That] (f: (Char) ⇒ B)(implicit bf: CanBuildFrom[WrappedString, B, That]) : That
?
WrappedString Scaladoc 2.8.1:
"This class serves as a wrapper augmenting Strings with all the operations found in indexed sequences.
The difference between this class and StringOps is that calling transformer methods such as filter and map will yield an object of type WrappedString rather than a String"
scala> import scala.collection.immutable.WrappedString
import scala.collection.immutable.WrappedString
scala> val s = new WrappedString("foo")
s: scala.collection.immutable.WrappedString = WrappedString(f, o, o)
scala> s.filter(x => true)
res1: scala.collection.immutable.WrappedString = WrappedString(f, o, o)
scala> s.map(x => x)
res2: scala.collection.immutable.IndexedSeq[Char] = Vector(f, o, o)
Alas, map returns a Vector and not a WrappedString. If I understand this correctly:
Filter works since it simply uses the newBuilder method, but map needs an implicit CanBuildFrom for WrappedString just like BitSet has. Is this a bug in code or documentation or am I missing something?
Also, the scaladoc simplified version doesn't make any sense to me:
def map [B] (f: (Char) ⇒ B) : WrappedString[B]
def map [B, That] (f: (Char) ⇒ B)(implicit bf: CanBuildFrom[WrappedString, B, That]) : That
Shouldn't it be:
def map [B] (f: (Char) ⇒ Char) : WrappedString
def map [B, That] (f: (Char) ⇒ B)(implicit bf: CanBuildFrom[WrappedString, B, That]) : That
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一个是一个 bug,应该在 2.9 中修复。
The first would be a bug, one that shall be fixed for 2.9.