-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
spec: clarify whether the spec guarantees that slicing an array reuses that array in the slice #31689
Comments
For the record, a slice of an array shares storage with the array. I agree that I don't see an explicit statement of this at https://golang.org/ref/spec#Slice_expressions . |
In the slice types section, it says:
|
@randall77 that section is about a slice initialisation, not about slice expressions. |
@ianlancetaylor I'm sorry, I should have called out that we observed that behavior. 👍 The debate came down to whether that was an implementation-specific behavior, or whether it was a spec-defined behavior. Thank you for the ACK and assignment. |
@zerkms a slice expression initializes and returns new slice over an existing backing array referenced by the operand slice https://play.golang.org/p/yfkn4tYP27w |
This may actually be covered in the section regarding slice expressions, although maybe the phrasing could use some tweaking to read better for non-English readers:
There's a little ambiguity when the operand is an array it doesn't have an underlying array, but is the array itself. |
@theckman The slice is the result, and it is what has an underlying array, always, and it is shared with the operand. The spec does not say that the operand has an underlying array too. |
@as Yes, thank you for restating what I said in my comment above. |
@theckman right back at ya! |
Change https://golang.org/cl/177139 mentions this issue: |
While trying to explain a behavior of slicing an array, and the associated resulting capacity, we realized the spec wasn't immediately clear as to whether slicing an array reuses that array. Making it hard to know if it's part of the spec, or an implementation defined detail.
While reading this section, I understood it to say that this array would be used in the slice but not everyone interpreted it that way:
Would you be able to clarify if the spec guarantees this behavior, or if it's implementation-specific? If the former, would it make sense to add an explicit note to the spec to clear up any confusion? If it's not guaranteed, is it worth calling that out as being implementation specific?
The text was updated successfully, but these errors were encountered: