Use cases for pattern matching
Sebastian Fischer
mail at sebfisch.de
Wed Nov 22 12:28:32 UTC 2023
Hello.
Regarding the new features for pattern matching in JDK 21 I have some
interesting use cases that highlight how they enable new ways to structure
programs:
https://github.com/sebfisch/java21-demo/wiki/Pattern-Matching
Is this the right place to discuss them?
The first example uses a file search program to illustrate how algebraic
data types are useful in stream pipelines, especially in handling errors as
values when using a functional programming style. The second example uses
tree-structured data and shows how core ideas from functional programming,
especially related to data traversal, can be integrated into Java,
leveraging streams and other functional programming patterns.
Interestingly, the operations for data traversal can be reused instead of
implementing specialized variants for each new tree structure.
I would be interested in what you expect about how the examples might
change with future developments of Project Amber, especially regarding
reconstruction expressions [1] and their relation to interfaces.
For example, the interface
interface Named { String name(); }
can be implemented automatically by a record
record Person(String name) implements Named {}
because the record component matches the signature of the interface.
It would be useful to be able to write
named with { name = "New Name" }
for all implementations of `Named` (not only `Person`) and I wonder how
`Named` could in the future be adjusted accordingly such that records still
implement it automatically.
Best,
Sebastian
[1]
https://mail.openjdk.org/pipermail/amber-spec-experts/2022-June/003461.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20231122/504741c4/attachment-0001.htm>
More information about the amber-dev
mailing list