Some thoughts on Member Patterns as parser developer

Brian Goetz brian.goetz at oracle.com
Mon Jun 3 16:20:08 UTC 2024



If I understand correctly, member patterns are a way to say "I declare a, possibly exhaustive, set of states that class that patterns are members of could take". This is a good thing obviously, but this only works if one has access to source code of the class one wants to declare states of.

To be more precise, using your terminology:

 - A _deconstruction pattern_ declares a state that all instances of the current class must be in.  (This derives from deconstruction patterns being total.)
 - An _instance pattern_ declares a state that some instances of the current class can be in.  Under the proposed “case pattern” approach, a set of such patterns can indicate that they cover all instances of the class.
 - A _static pattern_ declares a state that some instances of _some other_ class can be in.  Same story about “case pattern”, though it is far more likely people will make incorrect assumptions / implementations about exhaustiveness when they are working at arms length.

So I believe that what you want is handled by static patterns, optionally augmented with some mechanism for exhaustiveness.

I suspect that you are more interested in the exhaustiveness part than the static pattern part, but I think there are diminishing returns there.  Any statement of “this set of patterns is exhaustive” is not trustable, so the compiler always has to insert a synthetic default anyway.


What am I asking for / suggesting, is to also add the possibility to declare such set of states for external classes. More formally, I want to be able to state something like "I declare a set of states X that type Y could take in the subject area  Z, that could be exhaustive if asserted". Using an example with tokes, I could reformulate this as follows: "I declare a set of states TokenKinds that type String could take in the subject area "Tokens", that is exhaustive by assertion". Subject area is not something present in language semantics, more of a topic that unites this set of states. Applying to CompletableFuture, this feature could introduce something like:

switch (future) over AsyncStates {
case Completed(var result) -> ...
case Interrupted -> ...
case Failed(var ex) -> ...
}

Note that "over AsyncStates" asserts that only patterns from set of states AsyncStates could be present

This could help adapt existing APIs to new java paradigms and features without having to modify them directly, which is in many cases at least unpleasant. Also, this opens a world of possibilities for users, by enabling them to use custom patterns on types they cant access source code of. Not limited to the Java standard library, It could be also useful in frameworks, with tasks like checking if principal is anonymous/logged in/unauthorized, and a huge pile of other ways that people could apply this feature.

On Mon, Apr 29, 2024 at 6:27 PM Brian Goetz <brian.goetz at oracle.com<mailto:brian.goetz at oracle.com>> wrote:


On 4/29/2024 10:02 AM, Olexandr Rotan wrote:
> I think I did a really poor job expressing my thoughts in the first
> message. I will try to be more clear now, along with some situations I
> have encountered myself.
>
> Assume we have a stateless math expressions parser. For simplicity,
> let's assume that we split expressions into several types:
> "computation-heavy", "computation-lightweight", "erroneous" (permits
> null as input) and "computation-remote" (delegate parsing to another
> service via some communication protocol), and types can be assigned
> heuristically (speculatively). For some tasks, we need to perform some
> pre- and postprocessing around core parsing logic, like result
> caching, wrapping parsing into virtual thread and registering it in
> phaser etc., for others - log warning or error, or fail parsing with
> exception.

If you are envisioning side-effects, then I think you are already
abusing patterns.  Patterns either match, or they don't, and if they do,
they may produce bindings to describe witnesses to the match. Exceptions
are not available to you as a normal means of "something went wrong"; in
writing patterns (you should think of throwing an exception from a
pattern declaration as being only a few percent less drastic than
calling System.exit()).

Patterns, as explained in the various writeups, are the dual
(deconstruction) of certain methods (aggregations.)   I don't see the
duality here.  (If I had to guess, you're trying to bootstrap your way
to parser combinators with patterns, but that's a different feature.)
So I'm still not sure that you're using patterns right, so I still want
to focus on that before we start inventing new features.

Case patterns are a form of ad-hoc exhaustiveness, to be used only when
other sources of exhaustiveness (e.g., enums, sealed types, ADTs) fail.
It isn't clear to me yet that these other sources have failed.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20240603/4677690b/attachment-0001.htm>


More information about the amber-dev mailing list