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

Seq from unsafe wrapped array fails to deconstruct in head and tail with pattern matching #20562

Closed
jpork opened this issue Jun 12, 2024 · 3 comments

Comments

@jpork
Copy link

jpork commented Jun 12, 2024

The issue is with scalaVersion := "3.4.2".

The deconstruction of a seq in head and tail with pattern matching fails in case the sequence was created from an array with ArraySeq.unsafeWrapArray.

In case the seq is created by its apply method the deconstruction succeeds as expected.

In both cases the declared type and the values are identical, but pattern matching produces different results.

Failing Test:

  "seq from unsafe wrapped array fails to deconstruct in head and tail" in:
    import scala.collection.immutable.ArraySeq
    val array = Array(1, 2, 3)
    val seq: Seq[Int] = ArraySeq.unsafeWrapArray(array)

    val res = seq match
    case head :: tail =>
      head
    case _ => 0

    res should be (1)

Succeeding Test

"just seq succeeds to deconstruct in head and tail" in:
  val seq: Seq[Int] = Seq(1, 2, 3)

  val res = seq match
  case head :: tail =>
    head
  case _ => 0

  res should be (1)

@jpork jpork added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jun 12, 2024
@som-snytt
Copy link
Contributor

som-snytt commented Jun 12, 2024

:: is a (non-empty) List. It is not an extractor for arbitrary Seq.

scala> List(42, 27) match { case x :: xs => x }
           ^
       warning: match may not be exhaustive.
       It would fail on the following input: Nil
val res0: Int = 42

scala> Vector(42, 27) match { case x :: xs => x }
                                     ^
       error: constructor cannot be instantiated to expected type;
        found   : scala.collection.immutable.::[A]
        required: scala.collection.immutable.Vector[Int]

scala> (Vector(42, 27): Seq[Int]) match { case x :: xs => x }
scala.MatchError: Vector(42, 27) (of class scala.collection.immutable.Vector1)
  ... 30 elided

It's usually recommended to ask questions on chat before creating a ticket.

@som-snytt som-snytt closed this as not planned Won't fix, can't repro, duplicate, stale Jun 12, 2024
@som-snytt som-snytt added itype:invalid and removed itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jun 12, 2024
@jpork
Copy link
Author

jpork commented Jun 12, 2024

Whatever you decide its okay for me. Most probably its just a minor issue but it took me a while to figure it out.

For me it just seems to break this promise of functional programming to easily reason about functions by just inspecting their code. The headOfSeqOrZero function in my example looks not too suspect for me. Just this parameter type of Seq which provides the :: operator. Either the input contains at least one element then the head of the seq is returned otherwise it just yields 0. But for some sequences head is returned for others not, just dependent on the creation of the seq.

It looks somehow effectful to me. Give it a seq 1, 2, 3 it returns 1. Give it another seq 1, 2, 3 it returns 0. Hmm.

  "seq from unsafe wrapped array fails to deconstruct in head and tail" in:
    def headOfSeqOrZero(seq: Seq[Int]): Int = 
      seq match
      case head :: tail =>
        head
      case _ => 
        0

    //success
    headOfSeqOrZero(Seq(1, 2, 3)) should be (1)

    //failure
    import scala.collection.immutable.ArraySeq
    headOfSeqOrZero(ArraySeq.unsafeWrapArray(Array(1, 2, 3))) should be (1)

@som-snytt
Copy link
Contributor

som-snytt commented Jun 12, 2024

There is an extractor for Seq.

scala> Vector(42, 27) match { case x +: xs => x }
             ^
       warning: match may not be exhaustive.
       It would fail on the following inputs: Vector0, Vector1(), Vector2(), Vector3(), Vector4(), Vector5(), Vector6()
val res0: Int = 42

Just this parameter type of Seq which provides the :: operator.

This is your misunderstanding.

There are different flavors of patterns when pattern matching. It happens that :: is "just a case class", but +: is an "extractor".

Your match is really seq match case _: List => or more precisely case _: :: =>. One case is for list, the other for other seqs.

This would be a fruitful discussion on the discord chat or the discourse forum. They have people who are great at explaining things. https://scala-lang.org/community/ has links.

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