Re: “Deriving” functionality for classes and pattern matching on LHS of assignment?

Gavin Bierman gavin.bierman at oracle.com
Thu Dec 5 11:17:05 UTC 2024


I can speak to the second point: Yes, this is very much on our radar. There has been discussion of this on this list I believe (or maybe on the spec-experts list), and it is mentioned in early design documents that you can read on the Amber project page [1].

There are other places that we would like to consider to support patterns, e.g. in the head of lambda expressions and in enhanced for statements. We may well do these all in one go.

Thanks for your interest!
Gavin

[1]:  https://openjdk.org/projects/amber/design-notes/patterns/pattern-match-semantics
https://openjdk.org/projects/amber/


On 5 Dec 2024, at 09:34, vab2048 <vab2048 at gmail.com> wrote:

Hi Amber team,

I was wondering about the future of two different features and thought I would just ask the mailing list.

The first feature: deriving instances.

Haskell allows type declarations to derive instances for certain type classes (Eq, Ord, Enum, Bounded, Show, and Read).

Is it possible to get some sort of equivalent for java classes which would allow us to specify to the compiler to auto derive some code. I am thinking about easily auto-generating a toString, equals, hash code etc? We already have a form of this for records, and so bringing it to classes would make a logical next step.

The second feature: pattern matching on LHS of assignment.

Suppose we have this record type:

record Color(byte red, byte green, byte blue) {}

and a static factory method “someFactory” which returns a Color.

Are there any plans to allow us to pattern match at the point of assignment like so:

Color(byte red, byte green, byte blue) color = someFactory();

This would be a great addition to pattern matching.


Regards,

Vikram





-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20241205/e3056a1b/attachment.htm>


More information about the amber-dev mailing list