moonbitlang / core

MoonBit's Core library
https://moonbitlang.com/
Apache License 2.0
586 stars 73 forks source link

[Proposal] remove heavily suffix-depended methods for iterating `Array` #426

Open shamiao opened 4 months ago

shamiao commented 4 months ago

Array has the following methods to iterate:

... this sums up as 2 suffixes indicating 2 dimensions:

... and generates 2^2 = 4 variants.

This API design style is a bad smell for me, since it severely limits extensibility. For example, adding a variant of cancellation with this signature:

/// Iterates over the elements of the array. Return `True` for cancelling the iteration for remaining elements.
pub fn iter_with_break(self: Array[T], f: (T) -> Bool) -> Unit

Then all the existing variants must add _with_break suffix, bloating API to 2^3 = 8 variants:

As seen from this example, any new variants will cause the code bloats by O(2^n) and making it unable to maintain very fast.