Some thoughts on Member Patterns as parser developer
Olexandr Rotan
rotanolexandr842 at gmail.com
Mon Jun 3 14:00:05 UTC 2024
I have been recently looking through devox speeches, and one of the points
you have made reminded me about this conversation. The thing you said is
something like "if we had pattern matching back in java 8, we could have
implemented the following for the CompletableFuture" and showed an example
of switching over possible states (like interrupted, completed etc). And
what has crossed my mind is that this is almost what I have tried to
express.
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.
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> 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/02148042/attachment.htm>
More information about the amber-dev
mailing list