You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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.
What did you do?
https://play.golang.org/p/ORxhPRy1cl
What did you expect to see?
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?
Working around the non-assignability of appropriately covariant / contravariant channels usually needs another channel and goroutine: https://play.golang.org/p/b2JsBdvLy1.
The text was updated successfully, but these errors were encountered: