bazelbuild / starlark

Starlark Language
Apache License 2.0
2.44k stars 161 forks source link

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

Open Quarz0 opened 5 years ago

Quarz0 commented 5 years ago

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

laurentlb commented 5 years ago

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)

alandonovan commented 5 years ago

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.

josharian commented 5 years ago

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