Member Patterns -- the bikeshed

Brian Goetz brian.goetz at oracle.com
Thu Apr 4 22:55:32 UTC 2024



>
> if (let MyClassWithDeconstructor(var a, var b) = myThing) { ... }

There may ultimately be room for an imperative match construct. However, 
we have learned (sometimes, we've had to learn the same lesson more than 
once) that "frills" should come later, and that thinking about them too 
early will distort thinking about the more important concepts.  "Let" is 
clearly a frill, as it is largely equivalent to a switch, as you point 
out.  So, maybe later.  Or not.

> if (MyClass.staticPattern(var a, var b) matches someThing) { ... }

This ship sailed when we chose to overload `instanceof` for type 
patterns.  We considered this then, chose to stick with instanceof, but 
even if we were convinced it was a bad choice (which I'm mostly not), 
trying to switch horses midstream is much worse.

> Or, if the object's static type is known to be MyClass:
>
> if (myThing.instancePattern(var a, var b)) { ... }

Its cute, but it doesn't work.  When methods and patterns are paired -- 
which is likely to be the most common outcome -- the method invocation 
and pattern invocation look too similar, and, in the case of nilary 
methods/patterns, this approach falls apart completely as they are 
textually the same.


> # Identifying a member as a pattern
>
> The "pattern" keyword is fine, as long as it's sufficiently contextual 
> to not break existing usages. For example, `Pattern pattern = 
> Pattern.compile("...")` should still be a valid field declaration.

This is not our first rodeo.

> # Position of match candidate
>
> None of the above! As previously suggested:
>
> static<A, B> pattern of(A a, B b : Optional<T>)

Valid candidate.

> However, I see no need for "match" to take "patternName"[1]. The more 
> concise `match(BINDINGS)` is preferable to `match patternName(BINDINGS)`.

Be very careful with pronouncements like "is preferable".   I realize 
you probably mean "I find it preferable" (but also, there's probably a 
reason it didn't occur to you to phrase it this way), but the two mean 
vastly different things.


Score: 1 out of N  :)




More information about the amber-spec-observers mailing list