本当に、ここで重要な役割を果たしていない私たちは、簡素化することができます:
val r = null: String
r match {case s:String => println("s is null")}
あなたはマッチがまだ失敗のパターンを確認することができます。 null.isInstanceOf[String]
がfalse
です:
A type pattern T is of one of the following forms:
- A reference to a class C, p.C, or T#C. This type pattern matches any non-null instance of the given class...
isInstanceOf
も、このように振る舞う:s: String
のようなタイプのパターンがspecifically defined not to match null
あるためです。
r match {
case s: String => println("r is not null")
case null => println("r is null")
}
2)_
や変数などの "キャッチオール" パターンを使用する:あなたはnull
を一致させたい場合は
だから、あなたは
1)のパターンとまったくnull
使用することができます。
r match {
case s: String => println("r is not null")
case s => println("matches only if r is null or not a String")
}
それとも我々はバックを置けば、あなたは
0123を記述します。
r match {
case Row(s: String) => println("r contains non-null")
case Row(null) => println("r contains null")
}
私はあなたに同意するだけで、あなたの答えは少し混乱するかもしれません:) – eliasah
私は答えを広げました、うまくいけばそれは今より明確です。 –
これは非常に良い答えです。ありがとうアレクセイ! :) – eliasah