Destructuring / Patterns in for-each
August Nagro
augustnagro at gmail.com
Wed Jan 20 00:06:05 UTC 2021
Hello,
I wouldn't be surprised if this has been thought of / discussed before, but
I would love to some day use destructuring in for-each loops, kind of like
the for-of loop in JavaScript.
For example, instead of
record Point(double x, double y) {};
double greatestMagnitude = 0.0;
Point[] points = ...
for (Point p : points) {
greatestMagnitude =
max(greatestMagnitude, sqrt(pow(p.x(), 2) + pow(p.y(), 2)))
}
We could have:
for (Point(var x, var y) : points) {
greatestMagnitude =
max(greatestMagnitude, sqrt(pow(x, 2) + pow(y, 2)))
}
Not a huge improvement in this example, but there's a lot of code like
this. It would be a nice bonus if Map.Entry could be destructured, but I
don't see how that would be possible since it's an interface.
Regards,
August
More information about the amber-dev
mailing list