Open josharian opened 5 years ago
Similar case: Python evaluates enumerate
lazily, but starlark does not. They thus disagree about subscriptability of enumerate
expressions as well.
Another case: zip
.
Python behavior can be surprising:
>>> a = [1, 2, 3]
>>> b = reversed(a)
>>> list(b)
[3, 2, 1]
>>> list(b)
[]
>>> a = [1, 2, 3]
>>> b = reversed(a)
>>> a[0] = 4
>>> list(b)
[3, 2, 4]
I'm not sure we really want this.
[originally discussed at https://github.com/google/starlark-go/issues/127; it may be fruitful to read that issue first]
The spec says:
Python 2 and 3 made a different decision: It returns an iterable instead.
This affords possible performance optimizations by doing work lazily. See
https://github.com/google/starlark-go/issues/127#issuecomment-459080474 for an example.
On the other hand, starlark lacks
next
, which means that makingreversed
return an iterable removes the most convenient idiom for getting the last item of an iterable: In Python, it isnext(reversed(x))
, whereas for starlark it isreversed(x)[0]
.One conservative decision is to change the spec to return an iterable, even if in practice most implementations return lists. That makes it possible to add optimizations down the line.
Discuss. :)