Closed Jamesernator closed 7 years ago
I'd vote for 2.
a = b.flatten(c);
should be equivalent to
a = b;
for (let i = 0; i < c; ++i) {
a = a.flatten();
}
@michaelficarra in that case, that sounds like option 1?
Yes.
I think 1 or 2 is sensible; there is already a precedent for avoiding cycles when setting prototypes; but there's also lots of precedent for "letting infinite loops be infinite", so ¯\_(ツ)_/¯
Yeah, this is a weird edge case, and I don't know that it's going to be any more common than infinite loops resulting from getters or proxies, which are cases where it is impossible to short-circuit infinite loops. So my vote is also for 1.
Going with option 1.
Basically this issue is about what should happen in these sort've cases:
Some potential options I see are:
Continually flatten in an infinite loop (probably not a great idea but at least it prevents any arbitrary behavior)
Throw an error if an infinite loop would be caused (requires adding every seen array to a Set and throwing if the item has already been added to that Set)
Don't re-flatten things that have already been seen, the first two examples would effectively not change the list, the final example would wind up as just
[c]
(same Set cost as above but tries to do something rather than throwing)