Collections patterns
Brian Goetz
brian.goetz at oracle.com
Thu May 19 13:03:55 UTC 2022
> We may want to extract sub-parts of the array / collections by
> example, and i would prefer to have the same semantics and a similar
> syntax.
This is pretty vague, so I'll have to guess about what you might mean.
Maybe you mean: "I want to match a list if it contains the a subsequence
that matches this sequence of patterns", something like:
[ ... p1, p2, ... ]
There is surely room to have APIs that query lists like this, but I
think this is way out of scope for a pattern matching feature the
language. Pattern matching is about _destructuring_. (Destructuring can
be conditional.) An array is a linear sequence of elments; it can be
destructured by a linear sequence of patterns.
Maybe you mean: "I want to decompose a list into the head element and a
tail list".
In Haskell, we iterate a list by recursion:
len :: [a] -> Int
len [] = 0
len x:xs = 1 + len xs
But again, this is *mere destructuring*, because the cons operator (:)
is the linguistic primitive for aggregation, and [ ... ] lists are just
sugar over cons. So matching to `x:xs` is again destructuring. We
could try to apply this to Java, but it gets very clunky (inefficient,
no tail recursion, yada yada) because our lists are *built
differently*. Further further, arrays are another step removed from
lists even.
Or maybe you mean something else; if so, please share!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20220519/c9e89c40/attachment.htm>
More information about the amber-spec-experts
mailing list