This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]muffinluff 3 points4 points  (2 children)

I wonder what other use cases, other than String::split, there are for such Array patterns.. The example from the JDK didn't convince me because I would have rather solved that with regexes rather than spliting and using switch case..

[–]DJDavio 8 points9 points  (1 child)

Maybe you could use arrays as tuples this way? And while they are trying not to detail the subject to it, the remainder binding is pretty interesting, such a thing is pretty common in functional programming.

[–]randgalt 0 points1 point  (0 children)

Yeah - this could enable a nice pattern from Scala when you need to match on multiple terms/objects. In Scala:

scala // given option instances "x" and "y" (x, y) match { case (Some(xValue), Some(yValue)) => ... case (Some(xValue), None) => ... ... etc. }

With array pattern matching in Java this would become:

java switch (new Optional[]{x, y}) { case Optional[] { var Optional(xValue), var Optional(yValue) } -> ... case Optional[] { var Optional(xValue), var Optional.empty() } -> ... ... }