Description
https://tip.golang.org/ref/spec#Slice_expressions states that
The core type of a must be a string, array, pointer to array, or slice.
https://tip.golang.org/ref/spec#Appending_and_copying_slices states for append
that
The core type of s must be a slice of type []E. The values x are passed to a parameter of type ...E and the respective parameter passing rules apply. As a special case, if the core type of s is []byte, append also accepts a second argument with core type string followed by ....
and for copy
that
The core types of both arguments must be slices with identical element type. [...] As a special case, if the destination's core type is []byte, copy also accepts a source argument with core type string.
All three passages require that the types have core types. append and copy do allow for two different core types (string and []byte) for the two arguments, but they don't allow []byte | string
for the second argument. The compiler, however, special-cases []byte | string
in all these places, as can be seen in the following function, which compiles:
func foo[T []byte | string](x T) {
var dst []byte
dst = append(dst, x...)
_ = x[0:1]
copy(dst, x)
}
/cc @findleyr
Metadata
Metadata
Assignees
Labels
Type
Projects
Status