Skip to content
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

Can't mix and explicit arg with varargs returned by a function call #41975

Closed
magicalo opened this issue Oct 14, 2020 · 5 comments
Closed

Can't mix and explicit arg with varargs returned by a function call #41975

magicalo opened this issue Oct 14, 2020 · 5 comments

Comments

@magicalo
Copy link

What version of Go are you using (go version)?

$ go version
(whatever go playground is using)

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

(whatever go playground is using)

go env Output
$ go env
(go playground)

What did you do?

Why do variadic args not work when mixing an explicit value with varargs returned by a function (args returned from a function)

WORKS:
https://play.golang.org/p/JNs31SQmAXc

DOESN'T WORK:
https://play.golang.org/p/UbGrrHUtoPe
https://play.golang.org/p/fVmZr1cGcJc

What did you expect to see?

Expected it to compile and run.

What did you see instead?

./prog.go:9:28: multiple-value test() in single-value context

@magicalo
Copy link
Author

https://play.golang.org/p/tscYiZK493z
Works but is unnecessarily complex to do something so basic.

@go101
Copy link

go101 commented Oct 14, 2020

A multi-value tuple can't mix with other values as one argument list in a function call (or the source value list in an assignment).
And tuples are not slices.

@ianlancetaylor
Copy link
Contributor

This language behavior is designed and intended.

We don't use the issue tracker to discuss language issues. Use a forum. See https://golang.org/wiki/Questions. Thanks.

@magicalo
Copy link
Author

magicalo commented Oct 14, 2020

A multi-value tuple can't mix with other values as one argument list in a function call (or the source value list in an assignment).
And tuples are not slices.

I believe it is converted to a slice implicitly. Hence why the first example works as shown in the code fragment on Playground.
So your comment "And tuples are not slices." seems irrelevant and inaccurate at best.

@go101
Copy link

go101 commented Oct 15, 2020

The types of all elements of a slice are the same one,. For a multi-value expression, it is not true.

Yes, for the workable case (https://play.golang.org/p/JNs31SQmAXc), a multi-value expression (tuple) is expanded to multiple single values and these single values are then converted (appended) into a slice. You can also think the slice is converted from the tuple directly, but this is not the key of this issue. Actually, compilers may also convert (a multi-value expression + other value expressions) shown in the unworkable cases to a slice, if Go specification allows such conversions.

There are surely some reasons why the spec prohibits them. You can look for the reasons in some Go forms/groups. The current rules might be some over-stricter for some cases. If you do think the rules should be relaxed, you could make a formal proposal instead of reporting it as a bug.

@golang golang locked and limited conversation to collaborators Oct 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants