Instance pattern vs functional interface
Brian Goetz
brian.goetz at oracle.com
Tue Jan 26 14:34:36 UTC 2021
All good questions, (and all questions I've filled notebooks with
scribbles on!) I think we're almost, but not quite, able to start
thinking about these issues, but I'd like to reach tentative agreement
on some of the earlier issues first.
One could observe, as Scala has, that a pattern should be convertible to
a T -> Optional<U> sort of function shape. Except that, we want U to
potentially stand for multiple values. Clearly records play a part of
this story, but I don't want to rush down the path of "anonymous
records" and "record conversions" just yet, as tempting as they might be.
Similarly, Scala has explored, for better or worse, pattern objects.
(See, for example,
https://notes.backgroundsignal.com/Querying_a_Dataset_with_Scala_s_Pattern_Matching.html.)
While cool, this gets brain-bending pretty fast.
There are a whole host of follow-on questions. If I have a `pattern(U)`
on T, can I write a Stream method that lets me
stream // a Stream<T>
.match(p) // a Stream<U>, flatmapped through the pattern p
This is clearly something people would want to write; it's a variation
of flatMap. And what if our pattern yields multiple bindings? Can we
combine a pattern with a lambda that takes the bindings and wraps them
into something (maybe a record, maybe not)? I'd rather work these
questions from the perspective of things people want to do, rather than
from language machinery what-if.
But, I'd rather come to consensus on the stuff that's on the table
first, before we distract ourselves with the blue-sky explorations. So
let's put a pin in these.
On 1/26/2021 7:06 AM, Remi Forax wrote:
> As usual, when introducing a new feature, the complexity arise with the interactions with the other already existing features.
> We have instance patterns, we have functional interface,
> does those things can be mixed ?
>
> Is an interface with one abstract pattern a functional interface ?
> interface Matcher {
> pattern [String] is(String s);
> }
>
> Can i declare a lambda with bindings ?
> Matcher upperCase = [String] (String s) -> ...
>
> as an equivalent of
> Matcher upperCase = new Matcher() {
> pattern [String] match(String s) {
> ...
> }
> };
>
> So i can pattern match on it ?
> Object o = ...
> if (o instanceof upperCase.is(String value)) { ... }
>
> regards,
> Rémi
More information about the amber-spec-observers
mailing list