Open Randgalt opened 8 months ago
Can I also put the annotation "externally" to an existing class? I think, this is useful when using an external library, where the source should not be modified. -- If I can modify the source, I could use IntelliJ's refactoring "convert to record". OK, that would introduce API incompatibility. Then, the presented approach is good. However, I currently think, for self-maintained code this approach feels to require too much maintenance effort in comparison to the gain. 😅
This is a speculative PR. Open for discussion.
This is a variation of
@RecordInterface
that generates a general purpose tuple from any class. The impetus for this is to take advantage of Java 21's record patterns with non-record objects. In the future, Java will generally support deconstructors, but it doesn't today. Only realrecord
s are supported for record patterns. With the addition here of@RecordDeconstructor
and object can be use for record pattern matching.Example:
Given a legacy or non-record class like this:
We can now create a tuple-like record from this class by adding annotations:
This generates the following record:
Which can be used for pattern matching: