Set.empty上的匹配行为与"" .split(",")。toSet(PartialFunction)的行为不同

时间:2015-06-26 16:31:41

标签: scala

为什么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)

1 个答案:

答案 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()内是空字符串。 :)