[patterns] AND patterns, OR patterns, fall though

Mark Raynsford mark at io7m.com
Sun Nov 5 22:36:42 UTC 2017


On 2017-11-04T18:20:44 -0400
Brian Goetz <brian.goetz at oracle.com> wrote:
>
> Similarly, its even less clear that we need AND patterns.  Though I could imagine wanting intersection type-test patterns, like:
> 
>     switch (lambda) {
>         case Predicate p && Serializable: ...
>         case Predicate p: ...
>     }
> 
> Are there compelling use cases for AND patterns that I’m missing?  

I've given this more thought and I still can't find a reason for AND
patterns to exist at all. It seems that conjunctions of patterns that
would deconstruct values would either be unmatchable, or could be just
as easily expressed as single nested patterns. This is because if the
two sides of the AND pattern were contradictory, then the pattern could
never match. If the two sides weren't contradictory, then they could
obviously be expressed as a single pattern.

  // Assume an Option type that's Some(x) | None
  __data class T (Option<T> left, Option<T> right) { }

  switch (t) {
    // Obviously reducible to (Option(Some(x), Some(y)))
    case (Option(Some(x), _)) && (Option(_, Some(y))) -> ...

    // Obviously impossible
    case (Option(Some(x), _)) && (Option(None, _)) -> ...
  }

This would leave patterns that don't deconstruct values. Basically:
instanceof checks. It seems like those could be just as easily
expressed as guards/predicates on patterns (I think pattern guards
were discussed months back).

-- 
Mark Raynsford | http://www.io7m.com



More information about the amber-spec-experts mailing list