Intersection types in patterns

Mark Raynsford mark at
Sat Oct 20 16:10:10 UTC 2018

On 2018-10-20T11:40:39 -0400
Brian Goetz <brian.goetz at> wrote:
> One area where we have a question about whether to support an optional 
> binding variable is in deconstruction patterns:
>      case Point(var x, var y) p: ...
> Sometimes it is desirable to bind the cast target as well as its 
> components (Scala and Haskell support this with the `var at pattern` 
> syntax.)  Can anyone offer pros/cons here for supporting this based in 
> real-world experience?  (Assume someone has already said "I could 
> imagine how that might be useful.")

Is there a situation where you could have reached that case without
having a reference to p? If I've understood so far:

  SomeSuperTypeOfPoint q;

  switch (q) {
    case Point(var x, var y) p: ... // p == q and p : Point

So in the Point case there, you could do something with the fields
x, y (including possibly mutating them) but then pass the sharper-typed
p alias to some other method that can only take Points and not
SomeSuperTypeOfPoints? Is the type of q assumed to be Point in that
case (via the shiny new intelligent typing rules)?

How is the resulting declaration scoped? What if I do...

  SomeSuperTypeOfPoint q;

  switch (q) {
    case Point(var x, var y) p:      ... // p == q and p : Point
    case OtherPoint(var x, var y) p: ... // p == q and p : OtherPoint

I'm not sure how this would interact with the current
scoping/fallthrough rules.

In Haskell, @ patterns are often needed because of the support for
equational definitions that immediately match upon their arguments. For

  length :: forall a. [a] -> Integer
  length []       = 0
  length (x : xs) = 1 + (length xs)

In other words, there was never a binding in scope that referred to the
original list, only the deconstructed parts. I can't think of any case
in Java where that could happen. I guess what I'm saying is that the
con would be an extra set of syntax rules for something that might not
actually be useful in Java, as it doesn't have these sorts of
equational definitions and AFAIK there should always be a binding in
scope upon which the matching is being performed.

Mark Raynsford |

More information about the amber-spec-experts mailing list