-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: review/adjust conversion rules for string(x) where x is a generic type of the form []T (feature request) #50421
Comments
cc @griesemer |
Pet the current spec rules on generic conversions, this conversion is not permitted. Note that the following code does work: func toString[T []byte|[]rune](slice T) string {
return string(slice)
} Maybe the rules need to be relaxed. |
Thanks! As motivation for the rule relaxation, I'll present my use-case. I have a generic type Due to the current rules, I cannot just write the following: func ToString[T rune | uint8](iter Iterator[T]) string {
return string(ToSlice(iter))
} Trying to work around it doesn't work either: func sliceToString[T []rune | []uint8](slice T) string {
return string(slice)
}
func ToString[T rune | uint8](iter Iterator[T]) string {
return sliceToString(ToSlice(iter))
} And fails with
Which is hard for me to understand as This means that I'll have to write 2 |
Thanks. We're not going to change this for 1.18. In the long run I think we should probably permit this, but let's leave real consideration for later. Thanks. |
@griesemer Could you more specific on which line in the current spec disallows this conversion? I only found this line
The line doesn't mention type parameters. |
In your example |
Is this the same problem? type Bytes []byte
func hos[T Bytes](x []byte) T {
return x // okay
}
func lay[T []byte](x Bytes) T {
return x // error
}
func rez[T []byte](x Bytes) T {
return T(x) // okay
} [edit]: It is not the same problem. It is an assignment rule problem, |
Making this a proposal for language change. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes (1.18 beta)
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Compile the following code:
What did you expect to see?
Since
string(...)
can take either a[]byte
or a[]rune
, I expected the code to compile successfully.What did you see instead?
Compilation fails with:
See https://gotipplay.golang.org/p/ZgvdU6Xv9jB
The text was updated successfully, but these errors were encountered: