Skip to content

Fix #581: Add tests and adapt to named-base unapplySeq spec #5959

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions tests/pending/run/value-class-extractor-seq.check

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
final class StringExtract(val s: String) extends AnyVal {
def isEmpty = (s eq null) || (s == "")
def toSeq: Seq[Char] = s
def get = this
def length = s.length
def lengthCompare(n: Int) = s.length compare n
def apply(idx: Int): Char = s charAt idx
def head: Char = s charAt 0
def tail: String = s drop 1
def drop(n: Int): StringExtract = new StringExtract(s drop n)
def drop(n: Int): Seq[Char] = s drop n

override def toString = s
}
Expand All @@ -19,7 +20,8 @@ final class ThreeStringExtract(val s: String) extends AnyVal {
def apply(idx: Int): Char = s charAt idx
def head: Char = s charAt 0
def tail: String = s drop 1
def drop(n: Int): ThreeStringExtract = new ThreeStringExtract(s drop n)
def toSeq: Seq[Char] = s
def drop(n: Int): Seq[Char] = s drop n

override def toString = s
}
Expand All @@ -29,7 +31,7 @@ object Bippy {
def unapplySeq(x: Any): StringExtract = new StringExtract("" + x)
}
object TripleBippy {
def unapplySeq(x: Any): ThreeStringExtract = new ThreeStringExtract("" + x)
def unapply(x: Any): ThreeStringExtract = new ThreeStringExtract("" + x)
}

object Test {
Expand All @@ -39,9 +41,9 @@ object Test {
}

def g(x: Any): String = x match {
case TripleBippy(3 :: Nil, 3.0, 'b', chars : _*) => "1: " + chars
case TripleBippy(5 :: Nil, 5.0, 'b' | 'B', chars : _*) => "2: " + chars
case TripleBippy(_, _, chars : _*) => "3: " + chars
case TripleBippy(3 :: Nil, 3.0, Bippy('b', chars : _*)) => "1: " + chars
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently Dotty does not support TripleBippy as it's written before. I agree this is not a proper fix. We need to decide whether or not to suppor the Scala2 behavior.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. The spec is also inconsistent as it claims to support a super set of scalac patterns.

Copy link
Member

@smarter smarter Feb 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this test was changed in scala/scala@308ae2d#diff-f761b9d2bf9ec2f17bc00f552fc07314 when Scala 2.13 changed how name-based unapplySeq works: scala/scala#7068

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the link @smarter 👍

case TripleBippy(5 :: Nil, 5.0, Bippy('b' | 'B', chars : _*)) => "2: " + chars
case TripleBippy(_, _, Bippy(chars : _*)) => "3: " + chars
case _ => "nope"
}

Expand Down
3 changes: 3 additions & 0 deletions tests/run/value-class-extractor-seq.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Bip(1, 2, 3)
Bip(1, 2, c @ WrappedArray(3, 4, 5): _*)
class [I
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import scala.runtime.ScalaRunTime.stringOf

final class ArrayOpt[T](val xs: Array[T]) extends AnyVal {
def toSeq: Seq[T] = xs.toSeq
def length: Int = xs.length
def isEmpty = xs == null
def get = xs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing this change is needed because Array[T] only defines length apply and drop after implicit conversion to ArrayWrapper, which dotty doesn't perform in that case...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the new Spec requires toSeq as well.

def get = this
def apply(i: Int) = xs(i)
def drop(i: Int): Seq[T] = xs.drop(i)
}

object Bip {
Expand Down Expand Up @@ -44,7 +48,7 @@ object Bip {
object Test {
def f(x: Any) = x match {
case Bip(a, b, c) => s"Bip($a, $b, $c)"
case Bip(a, b, c : _*) => s"Bip($a, $b, c @ ${stringOf(c)}: _*)"
case Bip(a, b, c: _*) => s"Bip($a, $b, c @ ${stringOf(c)}: _*)"
case _ => "" + x.getClass
}

Expand Down