Some thoughts on Member Patterns as parser developer

Olexandr Rotan rotanolexandr842 at gmail.com
Mon Jun 3 17:30:46 UTC 2024


Yes, I am in fact talking about exhaustiveness (and possibly patterns
hierarchy that I have talked about initially). The "set of states" is just
a nominal aggregation of such patterns. I believe that in most cases,
patterns are aggregated by some feature (state of async op, state of
stream, empty/non-empty optional etc), this is just a possible way to let
the language know this information too for some purpose. I do believe that
syntax like switch (...) over SetOfStates is good for readability in case
there are many groups of patterns for the same type as the code. Also, it
allows to isolate group of patterns that represent subject area one is
switching over from any other patterns, wo there are no situations, where
programmer could think "Well, I checked if it matches 3 patterns from area
A, now lets see if it matches something from area B", which is kind of
mixing apples and hippos and is simply incorrect way to write code.

Now to the elephant in the room: exhaustiveness. I understand your concerns
about a whole world of places where error can occur, if you just let users
assert that set of states is exhaustive. On the other hand, I think you
also understand how much value this could bring for readability, code
structuring and exhaustiveness. So my opinion on whether the option to
assert exhaustiveness should exist or not: I really don't know. There could
be some herbivore compromises like throwing IllegalStateException in the
synthetic default branch, which both fits semantically and kind of solves
the problem, but this is still a compromise. This is kind of a double-edged
blade here and having things like that is not in Java's fashion.
Although,throwing an exception in case of incorrect assertion is too high a
price to pay to discard this feature completely, sacrificing enhanced
readability, structuring and exhaustiveness? I think this is up to you as
language designers to decide. I, as a regular user, would say that I would
be willing to pay that price if I had the option to. It is also a unique
feature to have, a selling point of Java in some sense.

Also, maybe it is just me, but this switch over syntax seems just really
neat and being really descriptive about what this switch
statement/expression is responsible for.

On Mon, Jun 3, 2024 at 7:20 PM Brian Goetz <brian.goetz at oracle.com> wrote:

>
>
> 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>
> 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/bb23fc03/attachment.htm>


More information about the amber-dev mailing list