starlark icon indicating copy to clipboard operation
starlark copied to clipboard

spec: unclear about type of parameter for reversed, enumerate, extend

Open Quarz0 opened this issue 6 years ago • 3 comments

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).

Quarz0 avatar Jun 26 '19 10:06 Quarz0

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)

laurentlb avatar Jul 01 '19 17:07 laurentlb

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.

alandonovan avatar Jul 01 '19 18:07 alandonovan

Somewhat related: https://github.com/bazelbuild/starlark/issues/29 (which grew out of https://github.com/google/starlark-go/issues/127)

josharian avatar Jul 03 '19 16:07 josharian