-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #10464: Match overridden definitions #10531
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 all commits
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
matched! | ||
matched! | ||
matched! | ||
not matched | ||
not matched |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import scala.quoted._ | ||
|
||
object MatchMac { | ||
|
||
inline def apply(inline any: Any): Unit = ${ printMacImpl('any) } | ||
|
||
def printMacImpl(any: Expr[Any])(using Quotes): Expr[Unit] = { | ||
import quotes.reflect._ | ||
val res = any match { | ||
case '{ ($f: Person).name } => "matched!" | ||
case _ => "not matched" | ||
} | ||
'{ println(${Expr(res)}) } | ||
} | ||
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 subtle semantics in quoted patterns may cause problems. Maybe the programmer can change to Reflection for such use cases? Or, instead of quoted patterns, add extractors to support pattern match on 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. Yes, less common more precise matching can and must be done using reflection. The intended semantics have not changed (#10464 (comment)), it was just a bug in the implementation. 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 see, thanks for the explanation 👍 |
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
trait Person: | ||
def name: String | ||
|
||
case class PersonA(name: String) extends Person | ||
case class PersonB(name: String) extends Person |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
@main def Test = { | ||
val personA: PersonA = PersonA("JoeA") | ||
val personB: PersonB = PersonB("JoeB") | ||
val person: Person = personA | ||
MatchMac(personA.name) | ||
MatchMac(personB.name) | ||
MatchMac(person.name) | ||
MatchMac(PersonA("JoeA").name) // optimized to MatchMac("JoeA") | ||
MatchMac(PersonB("JoeB").name) // optimized to MatchMac("JoeB") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The semantics still seem incomplete in this implementation.
Consider:
The quoted expression
e1
will not match a pattern like'{ ($x: B).foo }
, but it should.