-
Notifications
You must be signed in to change notification settings - Fork 164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clarify the += operator #182
Comments
Alternatively, is it like python's += (https://docs.python.org/3/reference/datamodel.html#object.__iadd__) where it's defined to first try using a type-specific in place add and falling back to normal addition if it's not supported? My understanding is the intention is that it is like python. That matches how bazel and starlark-java treats it, and I'd argue is the most appropriate behavior. It seems particularly important for bazel so that user expectations are met when they do starlark-go implements the fallback here: https://github.com/google/starlark-go/blob/7a1108eaa0124ea025e567d9feb4e41aab4bb024/starlark/interp.go#L200-L218 starlark-java implements the fallback here: https://github.com/bazelbuild/bazel/blob/master/src/main/java/net/starlark/java/eval/Eval.java#L454-L464 |
A frozen list should throw an error. I don't think existing Starlark implementations allow new types to define their own I think @adonovan wants to allow |
@laurentlb I agree with all three points. |
I think one of the things @ndmitchell wanted to clarify was that the spec explicitly says "if x refers to a list, the statement does not allocate a new list but instead mutates the original list in place, similar to x.extend(y)." That statement implies that you cannot do |
I think the spec should be changed to say"if x refers to a list and y is also a list..." to reflect our current intention. There remains the question of whether we should widen the special case to "list += iterable". (A Bazel |
So there seem to be two choices that need to be made for what
|
My preferences:
Yes. It's useful, and consistent with the behaviors of both x.extend(y) in Starlark, and of x += y in Python.
If y isn't iterable, x += y should be an error ("ytype is not iterable"). Again, this is consistent with extend in Starlark, and with the behavior of Python. The spec wording and implementation work for both changes should be straightforward. |
@adonovan - while the spec and implementation work is straightforward, I wonder how many things this breaks. In particular, as @cjhopman notes, Bazel users who do:
Currently get the behaviour |
You're absolutely right, I neglected the |
From the spec:
Questions:
x
is a frozen list? Is it then an error?If so, is the correct translation of
x += y
:The text was updated successfully, but these errors were encountered: