starlark
starlark copied to clipboard
spec: unclear about type of parameter for reversed, enumerate, extend
The spec says reversed(x) returns a new list containing the elements of the iterable sequence x in reverse order..
And here it says Dictionaries are iterable sequences, so they may be used as ....
Yet the Java implementation (like python) rejects dictionaries as arguments to reversed, while the other implementations (go, rust) accept them.
The same with enumerate and list.extend where they take an iterable sequence, yet Java rejects dictionaries, while Go and Rust accept them. (Python also accepts dictionaries passed to enumerate and extend, unlike reversed).
In Python3, we can pass a dictionary to enumerate and list.extend. However, it has the concept of reversible types - which we haven't in Starlark:
$ python3
>>> reversed({})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'dict' object is not reversible
I think we should update the Java version to allow dict everywhere we expect an iterable, including reverse().
(cc @alandonovan)
Should we define reversed as returning a list, or an opaque iterable (like the result of range)? If the latter, reversed(x) can avoid materializing a copy of list(x) if x is an indexable sequence.
I concur with your implicit point that a ReverseIterable interface (that dict would implement) is worth its implementation complexity.
Somewhat related: https://github.com/bazelbuild/starlark/issues/29 (which grew out of https://github.com/google/starlark-go/issues/127)