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

proposal: allow copy builtin function to copy a slice of undirected channels to a slice of directed channels #41810

Closed
scorsi opened this issue Oct 6, 2020 · 4 comments

Comments

@scorsi
Copy link

scorsi commented Oct 6, 2020

Hello,

The only one found solution to #41695 (and #40010 by extension) which is actually the only one safe is just to allow the builtin copy function from a slice of undirected channels to a slice of directed channels.

package main

func main() {
	src := []chan int{make(chan int)}

	var dest []<-chan int
	_ = copy(dest, src)
}

We got the following error: arguments to copy have different element types: []<-chan int and []chan int.

Today, the only one solution is :

package main

func myChanIntCopy(dest []<-chan int, src []chan int) int {
	elemCopied := 0
	for i, c := range src {
		if cap(dest) <= i {
			dest = append(dest, c)
		} else {
			dest[i] = c
		}
		lenCopied = i + 1
	}
	return elemCopied
}

func main() {
	var dest []<-chan int

	src := []chan int{make(chan int)}
	_ = myChanIntCopy(dest, src)
}

The idea came from @bcmills: #41695 (comment)

@gopherbot gopherbot added this to the Proposal milestone Oct 6, 2020
@go101
Copy link

go101 commented Oct 6, 2020

This proposal should be more general and relaxed to the cases in which implicit conversions are allowed from source element type to destination element type. For example:

package main

type T *int

func main() {
	var a [100]T
	var b = make([]*int, len(a))
	copy(b, a[:]) // error: arguments to copy have different element types: []*int and []T
}

@go101
Copy link

go101 commented Oct 6, 2020

Same problem for append:

package main

type T *int

func main() {
	var a [2]T
	var b = make([]*int, len(a))
	b = append(b[:0], a[0], a[1]) // ok
	b = append(b[:0], a[:]...) // error: cannot use a[:] (type []T) as type []*int in append
}

@ianlancetaylor
Copy link
Contributor

I think this is effectively a dup of #15209.

@scorsi
Copy link
Author

scorsi commented Oct 6, 2020

I think this is effectively a dup of #15209.

This is a dup. I did check for this and don't successfully found it (to many issues lol). It can be closed so.

@golang golang locked and limited conversation to collaborators Nov 11, 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

5 participants
@ianlancetaylor @scorsi @gopherbot @go101 and others