Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10715 from som-snytt/backport/19386-record
Record accessor is nilary not nullary [ci: last-only]
- Loading branch information
Showing
8 changed files
with
149 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,7 @@ public String someMethod() { | |
return s + "!"; | ||
} | ||
} | ||
|
||
record R0() {} | ||
|
||
record R4<T>(T t) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// javaVersion: 16+ | ||
object C { | ||
|
||
def useR0: Unit = { | ||
val r0 = new R0 | ||
} | ||
|
||
def useR1 = { | ||
// constructor signature | ||
val r1 = new R1(123, "hello") | ||
|
||
// accessors signature | ||
val i: Int = r1.i | ||
val s: String = r1.s | ||
val j: Int = r1.i() | ||
|
||
// method | ||
val s2: String = r1.someMethod() | ||
|
||
// supertype | ||
val isRecord: java.lang.Record = r1 | ||
|
||
() | ||
} | ||
|
||
def useR2 = { | ||
// constructor signature | ||
val r2 = new R2.R(123, "hello") | ||
|
||
// accessors signature | ||
val i: Int = r2.i | ||
val s: String = r2.s | ||
|
||
// method | ||
val i2: Int = r2.getInt | ||
|
||
// supertype | ||
val isIntLike: IntLike = r2 | ||
val isRecord: java.lang.Record = r2 | ||
|
||
() | ||
} | ||
|
||
def useR3 = { | ||
// constructor signature | ||
val r3 = new R3(123, 42L, "hi") | ||
new R3("hi", 123) | ||
|
||
// accessors signature | ||
val i: Int = r3.i | ||
val l: Long = r3.l | ||
val s: String = r3.s | ||
|
||
locally { | ||
val i: Int = r3.i() | ||
val l: Long = r3.l() | ||
val s: String = r3.s() | ||
} | ||
|
||
// method | ||
val l2: Long = r3.l(43L, 44L) | ||
|
||
// supertype | ||
val isRecord: java.lang.Record = r3 | ||
} | ||
|
||
def useR4: Unit = { | ||
val r4 = new R4(42) | ||
val n: Int = r4.t | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// javaVersion: 16+ | ||
trait IntLike { | ||
def getInt: Int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// javaVersion: 16+ | ||
record R1(int i, String s) { | ||
|
||
public String someMethod() { | ||
return s + "!"; | ||
} | ||
} | ||
|
||
record R0() {} | ||
|
||
record R4<T>(T t) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// javaVersion: 16+ | ||
public class R2 { | ||
final record R(int i, String s) implements IntLike { | ||
public int getInt() { | ||
return i; | ||
} | ||
|
||
// Canonical constructor | ||
public R(int i, java.lang.String s) { | ||
this.i = i; | ||
this.s = s.intern(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// javaVersion: 16+ | ||
public record R3(int i, long l, String s) { | ||
|
||
// User-specified accessor | ||
public int i() { | ||
return i + 1; // evil >:) | ||
} | ||
|
||
// Not an accessor - too many parameters | ||
public long l(long a1, long a2) { | ||
return a1 + a2; | ||
} | ||
|
||
// Secondary constructor | ||
public R3(String s, int i) { | ||
this(i, 42L, s); | ||
} | ||
|
||
// Compact constructor | ||
public R3 { | ||
s = s.intern(); | ||
} | ||
} |