-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #11008: Support generic tuples as a valid unapply result #14434
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -193,10 +193,22 @@ object Applications { | |
productSelectorTypes(unapplyResult, pos) | ||
// this will cause a "wrong number of arguments in pattern" error later on, | ||
// which is better than the message in `fail`. | ||
else if unapplyResult.derivesFrom(defn.NonEmptyTupleClass) then | ||
foldApplyTupleType(unapplyResult) | ||
else fail | ||
} | ||
} | ||
|
||
def foldApplyTupleType(tp: Type)(using Context): List[Type] = | ||
object tupleFold extends TypeAccumulator[List[Type]]: | ||
override def apply(accum: List[Type], t: Type): List[Type] = | ||
t match | ||
case AppliedType(tycon, x :: x2 :: Nil) if tycon.typeSymbol == defn.PairClass => | ||
apply(x :: accum, x2) | ||
case x => foldOver(accum, x) | ||
end tupleFold | ||
tupleFold(Nil, tp).reverse | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I overshot by thinking we should do a fold here. I think that's the cause of the failure. We should just traverse the type structure and do the various unwrapping calls ourselves, e.g dealiasing, unwrapping annotations, etc. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like the fold does the right thing here, the problem is that we didn't special-case for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, actually it might not be the fold. But it's nothing to do with unapplySeq. We just didn't carry over the Moving it has the same affect (though it keeps it separate from the product match). But that unapplySeq change isn't right. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not? Shouldn't we also support generic tuples as a return type of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The point of unapplySeq is to define an extractor that can do runtime checks on the length of the sequence and operate on it (apply/drop/toSeq). But generic tuples, like all tuples and case class (i.e. all products), have a known length and we consume them as a products or product-like, not as a sequence, by directly accessing their components. What use case were you thinking of? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E.g.: scala> object Foo:
| def unapplySeq(x: String): (Int, Seq[String]) = (x.length, x.toList.map(_.toString))
scala> "foo" match
| case Foo(1, c) => "One character " + c
| case Foo(x, xs*) => s"Many $x characters $xs" We might want to use a generic tuple there. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough. But if you want to support it you should add tests that it actually works. |
||
|
||
def wrapDefs(defs: mutable.ListBuffer[Tree], tree: Tree)(using Context): Tree = | ||
if (defs != null && defs.nonEmpty) tpd.Block(defs.toList, tree) else tree | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
hello | ||
hello and 10 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
object A: | ||
def unapply(s: String): String *: EmptyTuple = Tuple1(s) | ||
|
||
object B: | ||
def unapply(s:String): String *: Int *: EmptyTuple = Tuple2(s, 10) | ||
|
||
@main def Test = | ||
"hello" match | ||
case A(x) => | ||
println(x) | ||
"hello" match | ||
case B(x, y) => | ||
println(s"$x and $y") |
Uh oh!
There was an error while loading. Please reload this page.