Warning about an unchecked type argument in this Scala pattern match?

Here is some insight to what happens behind the scene. Consider this code: class Test { new Object match { case x: SeqInt => true } new Object match { case Seq(1) => true } } If you compile with scalac -Xprint:12 -unchecked you'll see the code just before the erasure phase (id 13). For the first type pattern you will see something like: synthetic> val temp1: java.lang.

Object = new java.lang.Object(); if (temp1. IsInstanceOfSeqInt()) For the Seq extractor pattern you will see something like: synthetic> val temp3: java.lang. Object = new java.lang.Object(); if (temp3.

IsInstanceOfSeqA()) { val temp4: SeqA = temp3. AsInstanceOfSeqA(); val temp5: SomeSeqA = collection.this.Seq. UnapplySeqA(temp4); // ... } In both cases, there is a type test to see if the object is of type Seq ( SeqInt and SeqA ).

Type parameters will be eliminated during the erasure phase. Thus the warning. Even though the second may be unexpected, it does make sense to check the type since if object is not of type Seq that clause won't match and the JVM can proceed to the next clause.

If the type does match, then the object can be casted to Seq and unapplySeq can be called RE: thoredge comment on the type check. May be we are talking about different things. I was merely saying that: (o: Object) match { case Seq(i) => println("seq " + i) case Array(i) => println("array " + i) } translates to something like: if (o.

IsInstanceOfSeq_) { // type check val temp1 = o. AsInstanceOfSeq_ // cast // verify that temp1 is of length 1 and println("seq " + temp1(0)) } else if (o. IsInstanceOfArray_) { // type check val temp1 = o.

AsInstanceOfArray_ // cast // verify that temp1 is of length 1 and println("array " + temp1(0)) } The type check is used so that when the cast is done there is no class cast exception Whether the warning non variable type-argument A in type pattern SeqA is unchecked since it is eliminated by erasure is justified and whether there would be cases where there could be class cast exception even with the type check, I don't know Edit: here is an example: object SeqSumIs10 { def unapply(seq: SeqInt) = if (seq. Sum == 10) Some(seq) else None } (Seq("a"): Object) match { case SeqSumIs10(seq) => println("seq. Sum is 10 " + seq) } // ClassCastException: java.lang.

String cannot be cast to java.lang.Integer.

Here is some insight to what happens behind the scene. Consider this code: class Test { new Object match { case x: SeqInt => true } new Object match { case Seq(1) => true } } If you compile with scalac -Xprint:12 -unchecked, you'll see the code just before the erasure phase (id 13). For the first type pattern, you will see something like: val temp1: java.lang.

Object = new java.lang.Object(); if (temp1. IsInstanceOfSeqInt()) For the Seq extractor pattern, you will see something like: val temp3: java.lang. Object = new java.lang.Object(); if (temp3.

IsInstanceOfSeqA()) { val temp4: SeqA = temp3. AsInstanceOfSeqA(); val temp5: SomeSeqA = collection.this.Seq. UnapplySeqA(temp4); // ... } In both cases, there is a type test to see if the object is of type Seq (SeqInt and SeqA).

Type parameters will be eliminated during the erasure phase. Thus the warning. Even though the second may be unexpected, it does make sense to check the type since if object is not of type Seq that clause won't match and the JVM can proceed to the next clause.

If the type does match, then the object can be casted to Seq and unapplySeq can be called. RE: thoredge comment on the type check. May be we are talking about different things.

I was merely saying that: (o: Object) match { case Seq(i) => println("seq " + i) case Array(i) => println("array " + i) } translates to something like: if (o. IsInstanceOfSeq_) { // type check val temp1 = o. AsInstanceOfSeq_ // cast // verify that temp1 is of length 1 and println("seq " + temp1(0)) } else if (o.

IsInstanceOfArray_) { // type check val temp1 = o. AsInstanceOfArray_ // cast // verify that temp1 is of length 1 and println("array " + temp1(0)) } The type check is used so that when the cast is done there is no class cast exception. Whether the warning non variable type-argument A in type pattern SeqA is unchecked since it is eliminated by erasure is justified and whether there would be cases where there could be class cast exception even with the type check, I don't know.

Edit: here is an example: object SeqSumIs10 { def unapply(seq: SeqInt) = if (seq. Sum == 10) Some(seq) else None } (Seq("a"): Object) match { case SeqSumIs10(seq) => println("seq. Sum is 10 " + seq) } // ClassCastException: java.lang.

String cannot be cast to java.lang.Integer.

The type check is just an optimisation in this case. – thoredge Sep 2 at 6:10 @thoredge, without the type check, you'd get a ClassCastException when the object is not of type Seq versus trying to match against subsequent clauses. – huynhjl Sep 2 at 6:30 I don't get a ClassCaseException if I change the object to Map(1 -> 2, 2 -> 3), Map("1" -> "2", "2" -> "3"); or "" for that matter – thoredge Sep 2 at 8:24 With -Xprint I get case collection.this.Seq.unapplySeqA() (ArrayA{1, 2, 3}) => ... I don't know what that is, but there is no instanceof check.

– Rob N Sep 2 at 11:27 @thoredge, you don't get a ClassCastException because the compiler added the isInstanceOf check. – huynhjl Sep 2 at 15:25.

Declaring the match object outside at least makes it go away, but I'm not sure why: class App object Test extends App { val obj = List(1,2,3) : Object val MatchMe = Seq(1,2,3) val res = obj match { case MatchMe => "first" case _ => "other" } println(res) }.

2 Yes good find. This is because in this case, it's using a stable identifier pattern, and those compile just to an equality test (per the language spec), no casting needed. If you want to bind variables, you're out of luck though.

– huynhjl Sep 2 at 6:27 Good to know, but yes, I need to bind variables. – Rob N Sep 2 at 11:24.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions