-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
cmd/compile: slice constraints prevent indexing #52129
Comments
As a workaround, this works: func presort1[T ~int | ~uint](slc []T) {
l, h := 0, 10
for h < len(slc) {
if slc[h] < slc[l] {
slc[h], slc[l] = slc[l], slc[h]
}
l++
h++
}
} |
That has a narrower scope, cannot accept MySlice for example |
No, that still works. https://go.dev/play/p/h3nx0geBIRf |
There is a principle rule: the type of a value must be a specified type, which might be either an ordinary type or a type parameter. In the first comment, [Edit] Another implementation variation: func presort1[T ~int | ~uint, S ~[]T](slc S) {
l, h := 0, 10
for h < len(slc) {
if slc[h] < slc[l] {
slc[h], slc[l] = slc[l], slc[h]
}
l++
h++
}
} |
This is expected according to the current spec. We may change it some day, but not soon. Closing. |
What version of Go are you using (
go version
)?1.18
Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?Linux Amd64
What did you do?
Tried to
go build
:What did you expect to see?
No errors.
What did you see instead?
presort1
does not compile with errorpresort2
is fineThe text was updated successfully, but these errors were encountered: