-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add support for java records in pattern matching #24140
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. Ideally we would not need this, since we don't need that information pickled and it's only relevant for the current compilation run. We could add a property key for a tree, but that would only work for the situations where we use the JavaOutlineParser, when we read the classfiles we would not have access to a tree on which a property could be used. I see we can really only get this information out when parsing the tree, and we don't save it anywhere else, which I guess justifies the annotation here. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package scala.annotation.internal | ||
|
||
import scala.annotation.StaticAnnotation | ||
|
||
/** An annotation attached by JavaParsers/ClassfileParser to Java record class | ||
* with a list of that record's fields. Used in pattern matching on records. | ||
*/ | ||
final class JavaRecordFields(args: String*) extends StaticAnnotation |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
empty | ||
hello | ||
hahaha | ||
hehehe | ||
21 | ||
hihihi | ||
hohoho | ||
unapply | ||
hejhejhejhej |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
public record Rec0_1() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
public record Rec1_1(String s) {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
public record Rec2_1(int x, String y) {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
public record Rec3_1<T>(int x, T y) {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
public record Rec4_1<T extends Comparable<Integer>>(int x, T y) {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
public record Rec5_1<T, U extends Comparable<T>, W extends Comparable<W>>(T t, U u, W w) {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
public record RecUnapply_1(int i, String s) { | ||
public static boolean unapply(RecUnapply_1 r) { return true; } | ||
} |
Florian3k marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
case class Foo(val value: String) extends Comparable[Integer]: | ||
override def compareTo(other: Integer) = 0 | ||
|
||
case class Bar(val value: String) extends Comparable[Bar]: | ||
override def compareTo(other: Bar) = 0 | ||
|
||
case class Baz(val s: String, val i: Int) | ||
|
||
object Baz: | ||
def unapply(b: Baz): Rec2_1 = Rec2_1(b.i + 1, b.s + "j") | ||
|
||
@main def Test = | ||
val r0 = Rec0_1() | ||
r0 match { case Rec0_1() => println("empty") } | ||
|
||
val r1 = Rec1_1("hello") | ||
r1 match { case Rec1_1(s) => println(s) } | ||
|
||
val r2 = Rec2_1(3, "ha") | ||
r2 match { case Rec2_1(i, s) => println(s * i) } | ||
|
||
// type param (no bounds) | ||
val r3a = Rec3_1(3, "he") | ||
r3a match { case Rec3_1(i, s) => println(s * i) } | ||
val r3b = Rec3_1(3, 7) | ||
r3b match { case Rec3_1(i, j) => println(i * j) } | ||
|
||
// type param with simple bounds | ||
val r4 = Rec4_1(3, Foo("hi")) | ||
r4 match { case Rec4_1(i, f) => println(f.value * i) } | ||
|
||
// type params with recursion / mutual reference | ||
val r5 = Rec5_1(3 : Integer, Foo("h"), Bar("o")) | ||
r5 match { case Rec5_1(i, f, b) => println((f.value + b.value) * i) } | ||
|
||
// custom unapply | ||
val r6 = RecUnapply_1(3, "x") | ||
r6 match { case RecUnapply_1() => println("unapply") } | ||
|
||
// scala class returning record from unapply | ||
val r7 = Baz("he", 3) | ||
r7 match { case Baz(i, s) => println(s * i) } |
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.
Doesn't matter, but a little cleaner to just return
Nil
here in my opinion