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: all: broader assignability to send/receive channel types #19079

Closed
DrJosh9000 opened this issue Feb 14, 2017 · 2 comments
Closed

proposal: all: broader assignability to send/receive channel types #19079

DrJosh9000 opened this issue Feb 14, 2017 · 2 comments

Comments

@DrJosh9000
Copy link
Contributor

What did you do?

https://play.golang.org/p/ORxhPRy1cl

What did you expect to see?

7
42

Since ProduceTo declares its argument as a chan<- int, it can only ever write ints (or close) to the channel. ints can be assigned to variables of type interface{}, and ints can be sent to a chan interface{}, so it'd be nice to let ProduceTo write to a chan interface{} even though its argument is chan<- int.

Similarly, since ConsumeFrom takes a <-chan interface{}, so it must be able to handle any interface{} value read from the channel. By passing a chan int value, the programmer knows that in reality ConsumeFrom will only receive int values, but ConsumeFrom is ready. ConsumeFrom can handle anything! Believe in ConsumeFrom!

What did you see instead?

tmp/sandbox755940094/main.go:17: cannot use c1 (type chan interface {}) as type chan<- int in argument to ProduceTo
tmp/sandbox755940094/main.go:22: cannot use c2 (type chan int) as type <-chan interface {} in argument to ConsumeFrom

Working around the non-assignability of appropriately covariant / contravariant channels usually needs another channel and goroutine: https://play.golang.org/p/b2JsBdvLy1.

@bradfitz
Copy link
Contributor

This is too big of a language change at this point. See previous discussion in https://github.com/golang/go/issues?utf8=%E2%9C%93&q=covariant

These sorts of issues will be reconsidered if/when there's a Go2.

@DrJosh9000
Copy link
Contributor Author

Thanks for taking a look anyway. I did a number of similar searches and found nothing specific to channel send/receive types, so figured it was worth a shot.

@golang golang locked and limited conversation to collaborators Feb 14, 2018
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

3 participants