You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Firstly, the following codes compile fine while run with exception:
scala>importscala.collection.SeqLikeimportscala.collection.SeqLike
scala>valxs:SeqLike[Char, String] ="abcdef"
xs: scala.collection.SeqLike[Char,String] = abcdef
scala> xs match { case head +: tail => tail }
java.lang.ClassCastException: java.lang.String cannot be cast to scala.collection.SeqLike
at .<init>(<console>:10)
at .<clinit>(<console>)
at .<init>(<console>:7)
at .<clinit>(<console>)
...
Secondly, there are two useful extractors defined in scala.collection.SeqExtractors.scala. But sadly these two extractors does not work for Array and String, and sometimes we do need these function, especially for String.
I would like the followed codes working as expected:
// Expected Result"abcdef"match { case p +: m :+ q => m } // expect m == "bcde"Array(1,2,3) match { case h +: tail => tail } //expect tail == Array(2,3)
Here I got an idea to fix the issue by rewriting these two extractors as following:
// SeqExtractors.scalapackagescala.collection/** An extractor used to head/tail deconstruct sequences. */object+: {
defunapply[T,Coll<%SeqLike[T, Coll]](t: Coll):Option[(T, Coll)] =if(t.isEmpty) NoneelseSome(t.head -> t.tail)
}
/** An extractor used to init/last deconstruct sequences. */object:+ {
/** Splits a sequence into init :+ tail. * @return Some((init, tail)) if sequence is non-empty. None otherwise.*/defunapply[T,Coll<%SeqLike[T, Coll]](t: Coll):Option[(Coll, T)] =if(t.isEmpty) NoneelseSome(t.init -> t.last)
}
The text was updated successfully, but these errors were encountered:
pt1 match {
// if at least one of the types in an intersection is checkable, use the checkable ones// this avoids problems as in run/matchonseq.scala, where the expected type is `Coll with scala.collection.SeqLike`// Coll is an abstract type, but SeqLike of course is notcaseRefinedType(ps, _) if ps.length >1&& (ps exists infer.isCheckable) =>Nonecase ptCheckable if infer isUncheckable ptCheckable =>valclassTagExtractor= resolveClassTag(pos, ptCheckable)
if (classTagExtractor !=EmptyTree&& unapplyMember(classTagExtractor.tpe) !=NoSymbol)
Some(classTagExtractor)
elseNonecase _ =>None
}
Firstly, the following codes compile fine while run with exception:
Secondly, there are two useful extractors defined in scala.collection.SeqExtractors.scala. But sadly these two extractors does not work for Array and String, and sometimes we do need these function, especially for String.
I would like the followed codes working as expected:
Here I got an idea to fix the issue by rewriting these two extractors as following:
The text was updated successfully, but these errors were encountered: