You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
prog.go:24:24: cannot convert ta (type []ARef) to type []Ref
Similar issues have been discussed but closed due to age or a valid reason. I have a Ref type which represents a technology ref. To differentiate a ref for each type, I created new type like ARef because I don;t want them to used one for the other. However I need to write several functions like findRef which operate on Ref type. Here one of the suggested way is to create a slice of Ref and copy elements, this is inefficient and not elegant. IMHO, a language should be consistent in different aspects. Allowing conversion of two types having the same underlying type but not allowing conversion of slices of the same types does not seem consistent.
Though I talked about slices of types in this description which I am practically facing right now, the problem can easily generalized by extended it to other complex type like pointers, array of pointers, maps etc.
The text was updated successfully, but these errors were encountered:
Like Ian says, this is covered well by the FAQ. If you have a specific proposal on how to fix this, see https://github.com/golang/proposal. But note that the issue isn't trivial; automatically converting slices could require allocations and copying in some cases.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
package main
import (
"fmt"
)
type Ref struct {
i int64
}
type ARef Ref
func findRef(refs []Ref, ref Ref) bool {
for _, r := range refs {
if r == ref {
return true
}
}
return false
}
func main() {
ta := []ARef{{0}, {1}}
found := findRef([]Ref(ta), Ref{0})
fmt.Println(found)
}
https://play.golang.org/p/pDZu77EZwEp
What did you expect to see?
The program should be compiled without error
What did you see instead?
prog.go:24:24: cannot convert ta (type []ARef) to type []Ref
Similar issues have been discussed but closed due to age or a valid reason. I have a Ref type which represents a technology ref. To differentiate a ref for each type, I created new type like ARef because I don;t want them to used one for the other. However I need to write several functions like findRef which operate on Ref type. Here one of the suggested way is to create a slice of Ref and copy elements, this is inefficient and not elegant. IMHO, a language should be consistent in different aspects. Allowing conversion of two types having the same underlying type but not allowing conversion of slices of the same types does not seem consistent.
Though I talked about slices of types in this description which I am practically facing right now, the problem can easily generalized by extended it to other complex type like pointers, array of pointers, maps etc.
The text was updated successfully, but these errors were encountered: