为什么Set.empty
与"".split(",").toSet
中的PartialFunction
行为不同?
val x = Set.empty
val y = Set("foo")
val z = "".split(",").toSet
val fooBar: PartialFunction[String, Int] = {
case "foo" => 5
case "bar" => 6
}
y.map(fooBar) // works
x.map(fooBar) // works
z.map(fooBar) // scala.MatchError: (of class java.lang.String)
答案 0 :(得分:2)
两者并不相同:
Set-Mailbox -identity s1112223 -Alias $SamAccountName ("{0}.{1}" -f $_.givenname, $_.sn)
scala> val x = Set.empty
x: scala.collection.immutable.Set[Nothing] = Set()
scala> x.size
res0: Int = 0
scala> val z = "".split(",").toSet
z: scala.collection.immutable.Set[String] = Set("")
scala> z.size
res1: Int = 1
行生成一个由一个元素组成的集合,该元素是空字符串。您在输出中只看到split
但实际上在Set()
内是空字符串。 :)