-
Notifications
You must be signed in to change notification settings - Fork 18k
Slice unpacking to variadic function not honored #29767
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
Comments
I'm not completely sure what you are saying, but I think you may be misreading the paragraph in the spec. The spec says that if you write It sounds like you may be saying that by using the trailing Closing because this is indeed working as intended. |
You may want to read the discussion in #18605. |
If In addition, the way that the FAQ section is worded suggests that if slice
I don't see the creation of that new slice happening, which is why I reported it. If not a defect in From what I understand #18605 appears to propose adding support for |
I mentioned #18605 because the discussion on that issue discusses the point about slices being passed directly. The language spec says, in the section "Passing arguments to The second of the spec that you quote above, which discusses a new slice, is the section in which the arguments are passed individually, without using |
What version of Go are you using (
go version
)?go1.10.4 linux/amd64
Does this issue reproduce with the latest release?
Haven't tried.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Defined function with signature:
func variadic(args ...interface{})
and attempted to call it by unpacking a slice:variadic([]int{1,2,3}...)
.What did you expect to see?
Compilation succeeds.
What did you see instead?
Conversion error from []int to []interface{}.
The reason why is understood, as per FAQ:
However, the first sentence suggests that this optimization should not have occurred because
[]int
is not assignable to[]interface{}
:False,
[]int
is not of type[]interface{}
.False, both types are defined.
False,
[]int
is not an interface.False,
[]int
is not a channel.False,
[]int
is notnil
.False,
[]int
is a type.Since
[]int
is not assignable to[]interface{}
the slice should have been unpacked and each element passed individually to the variadic function.There are several related issues (eg. #1616, #18547, #27795) that expose the same problem, probably more but none appear to go further than claiming that this is "working as intended".
If I wanted to pass the []int as-is, I could have simply omitted unpacking by writing
variadic([]int{1,2,3})
instead ofvariadic([]int{1,2,3}...)
. By typing the latter, I explicitly say "I want to unpack this slice". The compiler however decided to take my statement as suggestion since it believed[]int
is assignable to[]interface{}
and decided not to not honor my request, instead passing the slice as-is.I this believe is in error.
The text was updated successfully, but these errors were encountered: