On 2018-10-20T11:40:39 -0400
Brian Goetz <brian.go...@oracle.com> 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@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
example:

  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 | http://www.io7m.com

Reply via email to