Jen pro info... java ma pattern matching...
Měl jsem dojem, že umí matchovat jenom typ objektu...
Ma Guarded patterns a destructuring (aspon ve switch):
record Person(String firstname, String surname) {}
record Couple(Person p1, Person p2) {}
public static void main(String[] args) {
Person daddy = new Person("John", "Doe");
Person mommy = new Person("Jane", "Doe");
Person uncle = new Person("John", "Apples");
var objects = Arrays.asList(
new Couple(daddy, mommy),
new Couple(mommy, daddy),
new Couple(uncle, daddy),
10,
"Random string");
objects.stream()
.map(i -> switch (i) {
case Couple(Person p1, Person p2)
when p1.firstname.equals(p2.firstname) -> (Supplier<String>) () -> "Split it in half between " + p1 + " and " + p2;
case Couple c
when c.p1.firstname.equals("John") -> (Supplier<String>) () -> "Daddy is paying";
case Couple c
when c.p1.firstname.equals("Jane") -> (Supplier<String>) () -> "Mommy is paying";
case Integer ignored -> (Supplier<String>) () -> "What are you doing here Integer";
case Object ignored -> (Supplier<String>) () -> "Runners... call 911";
})
.forEach(i -> System.out.println(i.get()));
}
a value objects (primitive classes) jsou na ceste... JEP 401
To je zajímavé, je fajn, že se něco děje po tom, co vallhala nedopadla... Nicméně v tomhle případě by nám to nepomohlo, protože jestli to dobře chápu, ty typy nebudou moct být generické...
Myslim, ze je to porad valhalla... A nevim jestli budou moct byt genericke... spis ne