-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Whether support *:
as a valid return type of unapply
in extractors
#11008
Comments
We could use the
This is part of a different dimension of genericity that we need to be able to handle as well. |
I agree, if we can find a way to change the specification without a special case for |
Is there a strong reason why |
|
The last two items seem to be not strong reasons. The first can be fixed by changing the return type. For the second, I don't think realistic code would write pattern match to deal with > 22 arguments. |
I'd add - support extractors for generic code. |
@joroKr21 To check whether it affects the specification, could you be more specific about this with a simple example? |
@liufengyun I can give you a complex example of what I would like to try in Scala 3: recursion-schemes.scala The idea is that for a recursive type ( But to use them we need to pattern match on I will think a bit about a simpler example. |
How does Scala 3 support So if Scala 3 generalised that into rewriting any object *: {
def unapply[H, T <: Tuple](x: H *: T): (H, T) = (x.head, x.tail)
} |
Dotty has some similar logic in exhaustivity check: However, this is just a trick in exhaustivity check: type checking and semantic translation don't have such things. The following semantic rewrite seems to be problematic, as it throws
|
In the meeting, it's decided that we need to support The initial plan is to add an ad-hoc rule in the specification of pattern matching. Typer, patternMatcher, exhaustivity check needs to be updated accordingly. |
Fix #11008: Support generic tuples as a valid unapply result
This could become more likely if scala/improvement-proposals#44 goes through |
Please tell me if I post in the wrong place. In the following code: class myClass[A<:Tuple]:
def unapply(n:Int):Option[A] =
None
def add[B]:myClass[Tuple.Append[A,B]] =
new myClass[Tuple.Append[A,B]]
@main def test =
val val1 = myClass[(Int,Double)]()
val val2: myClass[(Int, Double, String)] = val1.add[String]
42 match
case val2(n,d,s) => println("")
case _ => println("ok") if I don't give the type for val2 in line 8, the compiler correctly infers its type, but extra parentheses are needed line 10 around (n,d,s). |
The following code is currently not supported by the compiler:
If we change
String *: EmptyTuple
toTuple1[String]
, it will compile without problem.The problem is that according to the current specification,
String *: EmptyTuple
does not qualify as a product pattern, as it does not contain selectors_1, ..., _N
.The question is:
String *: EmptyTuple
?One particular concern is that making
*:
a special case seems to cancel the past efforts to make pattern match more generic.Related PR: #10987
The text was updated successfully, but these errors were encountered: