Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An improvement to make seq's extractors work for Array and String #7108

Open
scabug opened this issue Feb 10, 2013 · 2 comments
Open

An improvement to make seq's extractors work for Array and String #7108

scabug opened this issue Feb 10, 2013 · 2 comments

Comments

@scabug
Copy link

scabug commented Feb 10, 2013

Firstly, the following codes compile fine while run with exception:

scala> import scala.collection.SeqLike
import scala.collection.SeqLike
scala> val xs: 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.scala
package scala.collection

/** An extractor used to head/tail deconstruct sequences. */
object +: {
  def unapply[T,Coll <% SeqLike[T, Coll]](t: Coll): Option[(T, Coll)] =
    if(t.isEmpty) None
    else Some(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.
   */
  def unapply[T,Coll <% SeqLike[T, Coll]](t: Coll): Option[(Coll, T)] =
    if(t.isEmpty) None
    else Some(t.init -> t.last)
}
@scabug
Copy link
Author

scabug commented Feb 10, 2013

Imported From: https://issues.scala-lang.org/browse/SI-7108?orig=1
Reporter: Eastsun (eastsun)
Affected Versions: 2.11.0-M1

@scabug
Copy link
Author

scabug commented Feb 18, 2013

@paulp said:
How can this possibly be right:

  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 not
    case RefinedType(ps, _) if ps.length > 1 && (ps exists infer.isCheckable) =>
      None

    case ptCheckable if infer isUncheckable ptCheckable =>
      val classTagExtractor = resolveClassTag(pos, ptCheckable)

      if (classTagExtractor != EmptyTree && unapplyMember(classTagExtractor.tpe) != NoSymbol)
        Some(classTagExtractor)
      else None

    case _ => None
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants