Skip to content

proposal: Go 2: allow any compatible slice argument to be copied automatically at the call site #27956

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

Closed
pjebs opened this issue Oct 1, 2018 · 3 comments
Labels
FrozenDueToAge LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change
Milestone

Comments

@pjebs
Copy link
Contributor

pjebs commented Oct 1, 2018

func main() {
	
	var (
		a int = 1
		b int = 2
		c int = 3
	)
	F(a, b, c) // Works
	F(1, 2, 3) // Works
	
	Inums := []interface{}{5, 6, 7}
	F(Inums...) // Works

	nums := []int{5, 6, 7}
	F(nums...) // Doesn't work
}

func F(x ...interface{}) {
	fmt.Println(x...)
}

Compiler Error:

cannot use nums (type []int) as type []interface {} in argument to F
@pjebs
Copy link
Contributor Author

pjebs commented Oct 1, 2018

I think for Go 2, a nice change would be to allow the above example to work as syntax sugar.

I understand they are technically different types (hence the error). I'm not saying the underlying data types or how the underlying data type works should be changed.

My proposal is only from a "verbal" point of view. The compiler just "pretends" each item in the slice was "by hand" written out in such a way that it simulates the way that it currently works.

If the way interfaces work causes issues, then my proposal is purely to consider interface{ } as a special case.

My proposal is also for it to work irrespective of what happens with the generics issue.

@ianlancetaylor ianlancetaylor changed the title Go 2 proposal: Allow any type of slice to work proposal: Go 2: allow any type of slice to work Oct 1, 2018
@gopherbot gopherbot added this to the Proposal milestone Oct 1, 2018
@ianlancetaylor ianlancetaylor added LanguageChange Suggested changes to the Go language v2 An incompatible library change labels Oct 1, 2018
@ianlancetaylor
Copy link
Contributor

See the discussion in #18605, especially #18605 (comment) .

@robpike robpike changed the title proposal: Go 2: allow any type of slice to work proposal: Go 2: allow any compatible slice argument to be copied automatically at the call site Oct 2, 2018
@ianlancetaylor
Copy link
Contributor

Adopting this as suggested would cause a subtle change in the handling of ...: if the types match exactly the same slice would be passed, but if the types differ then a new slice would be created and that new slice would be passed.

Also, as a general guideline we try to avoid having a simple statement cause a lot of work, but this could cause a simple call F(s...) to copy a large slice, converting each element of that slice.

It's always possible to write a little helper function to do this kind of work.

For those reasons, we aren't going to adopt this proposal. Thanks for sending it.

@golang golang locked and limited conversation to collaborators Oct 30, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change
Projects
None yet
Development

No branches or pull requests

3 participants