Scala: Strange MatchError when unapplying tuple with nulls -


can well-versed in scala explain why works:

scala> tuple2[string,string]("w3wre", "werffd") res0: (string, string) = (w3wre,werffd)  scala> val (s1:any, s2:any) = tuple2[string,string]("w3wre", "werffd") s1: = w3wre s2: = werffd 

but not this?

scala> tuple2[string,string]("w3wre", null) res1: (string, string) = (w3wre,null)  scala> val (s1:any, s2:any) = tuple2[string,string]("w3wre", null) scala.matcherror: (w3wre,null) (of class scala.tuple2)     @ .<init>(<console>:9)     @ .<clinit>(<console>) ... 

(clearly any-type can contain nulls:

scala> val n:any = null n: = null  scala> val n:any = null.asinstanceof[string] n: = null 

)

?

the language specification explicitly says such type patterns not match null (8.2 type patterns, emphasis mine):

type patterns consist of types, type variables, , wildcards. type pattern t of 1 of following forms:

  • a reference class c, p.c, or t#c. type pattern matches non-null instance of given class.

however, don't know language designers' reasoning behind not matching null.


Comments