JEP 305 - Pattern Matching for instanceof (Preview)

Attila Kelemen attila.kelemen85 at gmail.com
Wed Nov 27 18:47:00 UTC 2019

```Hi Gavin,

Originally I was thinking xor, though I forgot my original thought and
just wrote `!=` and `==`, and merely for the sake of completeness of
the binary operators. To be honest - without the record pattern
matching, and switch - I can realistically imagine using `if (a
instance C x)` constructs only, and nothing more complicated (mostly
as a deliberate decision to avoid too complex expressions). So, I
don't feel too bad not having rule for xor and such.

That said, you can create non-empty T/F sets, by writing this:

(!(a instanceof String x) == (b instanceof String x))

or the same with xor

!(!(a instanceof String x) ^ (b instanceof String x))

This when evaluates to true imply that exactly one of them can be cast
to `String` (and is consistent with my rules). While the false path
creates an empty binding set.

But as I wrote, this is all for the sake of completeness, I don't have
any realistic use-case where I would want to use it.

Attila

>
> Hi Attila:
>
> > One little comment on the "flow scoping" rules:
> > As I can see `x = y` empties the binding sets (for both T and F).
> > Is there a reason not to apply the same for them as if they were
> > written as `(x && y) || (!x && !y)`. That is, the following:
> >
> > T: intersect(union(x.T, y.T), union(x.F, y.F))
> > F: union(intersect(x.F, y.F), intersect(x.T, y.T))
> >
> > Obviously, this would apply for boolean x and y only,
> > and the reverse for `x != y`
>
> I appreciate the suggestion, thank you.
>
> Unfortunately, there are a couple of problems. First, it is important to note that flow scoping applies only to pattern variables. So your suggestion as written doesn’t really apply. Even if it could, your observation also kills it - it would only “..apply for boolean x and y”. Scoping of variables happens way before typing - indeed, you can’t define typing without knowing scoping.
>
> We can construct a similar example to consider though; for example:
>
> (e instanceof T x) == (f instanceof S y)
>
> You define the True set as:
>
> T: intersect(union(x.T, y.T), union(x.F, y.F))
>
> So, in this case it’s:
>
> intersect(union({x}, {y}), union({},{}))
> = intersect({x,y}, {})
> = {}
>
> Similarly for the False set. In fact, I think this will always be the case.
>
> I would be very interested to hear any feedback you may have of using pattern matching in anger in the next few months. We really want to hear what works and, please!, what is awkward - that’s the purpose of ‘preview’ features.
>
> Best wishes,
> Gavin
>
>
>
>
>
>
```