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: an uninitialized slice (and other types) is nil, yet a slice variable is initialized to its zero (nil) value #20186
Comments
In a similar vein:
I think the spec basically treats "uninitialized" and "nil" as equivalent. In particular, it does specify that a slice-literal, However, especially with the issues originally brought up here, I don't really feel confident to judge whether these actually yield consistent results… |
@alercah Thanks for this issue, I finally took a look at this. I think you're absolutely right about the initialization problem. In fact, the spec says elsewhere that variables which have no initialization expression are initialized to their zero value, which would be The same is true for pointers (an uninitialized pointer is Regarding the capacity of an explicitly reduced slice: You're right that in the implementation the length of the array beyond the slice is (may be) longer than what the reduced capacity claims it is, but there's no way to access that part of the array from that slice, so it might as well not exist (and theoretically, one could imagine a system that actually shrinks that underlying array). So I'm not sure there's much to do here. Retitling this issue to be clearer what it's about. |
@Merovius Regarding:
Here the maximum element index is considered in the abstract - it's the highest index of any element present. Even if there are literal element indices, not all elements are required to have one, and the element with the largest actual index may not have literally an index in the source. But perhaps this can be formulated a bit clearer.
You're correct, in a mathematically exact sense this is underspecified. Maybe this can be written down w/o the spec coming across overly pedantic.
Fair enough. I'd say, this is similar to your 2nd point. We've been trying to keep the spec reasonably readable, probably at the cost of some precision. Will try to address when we try to fix this issue. Not urgent. |
With respect to the second issue, I don't think there's nothing to be done. Perhaps focusing on the language about the underlying array was a mistake there. Regardless, though, slice expressions that don't explicitly specify the capacity of the resulting slice and I believe that they should. |
@alercah Ah, I see your point. Yes, it's unclear what the size of the underlying array is in these cases and this muddles the notion of capacity. Agree that this should be clearer. Thanks. |
I have two small issues with the definition of a slice type:
This is not true of a
nil
slice. Since this refers to initialization, it is talking about variables and not values. Changing this to "A non-nil
slice value..." would probably fix this, since initialization of slices is already well-defined elsewhere.This is not true of a slice whose capacity has been explicitly reduced. This is easily fixed by removing the explicit definition of capacity here and then, in the section on slice expressions, explicitly saying that the capacity of the slice
a[low:high]
iscap(a) - low
. This also solves an ambiguity in that the capacity ofa[:]
is not actually defined and, based on the quoted statement, could be argued to extend the capacity back of up that of the underlying array ifa
is a limited-capacity slice.The text was updated successfully, but these errors were encountered: