Closed rogpeppe closed 3 years ago
Personally I'd prefer that we use the length of the slice.
If a[i]
would panic for the original slice, then I think (*A)(a)[i]
should either panic or be statically disallowed.
the length of the slice must equal the length of the pointed-to array
Why == instead of >=? The conversation above has pretty consistently discussed >=.
To simplify measuring opinions here, I recommend:
On Bryan's last comment (2 above this one), vote:
On Josh's last comment (1 above this one), vote:
>=
==
Seems like there are some clear trends. :) I'll update the CLs.
@josharian I didn't see any release notes related to this. Could you make sure any of the changes get a RELNOTES=yes Tag so it won't be forgotten?
@nightlyone done. (None of them have been submitted yet, waiting on reviews.)
What about allowing conversion from *T
to *[1]T
as well? I would find that quite useful when trying to call a function that accepts a slice, but I have a single item already allocated from elsewhere.
@betawaffle, that's an interesting idea, but this proposal was already approved as-is — want to file a separate proposal for converting between *T
and *[1]T
?
@bcmills: Thanks for the suggestion. Opened #45545.
Change https://golang.org/cl/312070 mentions this issue: cmd/compile: allow export/import OSLICE2ARRPTR
Change https://golang.org/cl/315169 mentions this issue: cmd/compile/internal/types2: slice-to-array-pointer conversion requires go1.17
Change https://golang.org/cl/325549 mentions this issue: doc/go1.17: document slice to array pointer conversion
Change https://golang.org/cl/327649 mentions this issue: cmd/compile: allow ir.OSLICE2ARRPTR in mayCall
Change https://golang.org/cl/334669 mentions this issue: reflect: add Value.CanConvert
Change https://golang.org/cl/336890 mentions this issue: doc: clarify non-nil zero length slice to array pointer conversion
Change https://golang.org/cl/338630 mentions this issue: compiler, runtime: allow slice to array pointer conversion
Change https://golang.org/cl/339329 mentions this issue: compiler: check slice to pointer-to-array conversion element type
the length of the slice must equal the length of the pointed-to array
Why == instead of >=? The conversation above has pretty consistently discussed >=.
Maybe it's a bit too late to comment since the feature is already landed on Go 1.17, but I just want to point out that the >= behavior might lead to a hidden bug due to a different behavior expected by many programmers, i.e. they expect the conversion will success only if the lengths are equal. It violates the principle of least surprise.
The == behavior is also more explicit, which is usually preferred by the Go community, than the >= behavior: If the original slice's length is larger than the destination array's length, the slice needs to be first resized to the array's size.