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: Go 2: allow conversion of defined types of directional channels #37691

Closed
Anaminus opened this issue Mar 5, 2020 · 2 comments
Closed
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@Anaminus
Copy link

Anaminus commented Mar 5, 2020

Consider the following program:

package main

func main() {
	type Channel chan int
	type SendChannel chan<- int
	type ReceiveChannel <-chan int

	var channel Channel
	var send SendChannel
	var receive ReceiveChannel
	_, _, _ = channel, send, receive

	// Not allowed:
	send = SendChannel(channel)       // cannot convert channel (variable of type Channel) to SendChannel
	receive = ReceiveChannel(channel) // cannot convert channel (variable of type Channel) to ReceiveChannel

	// Allowed:
	send = SendChannel((chan<- int)(channel))
	receive = ReceiveChannel((<-chan int)(channel))
}

As demonstrated, it is not allowed to convert a defined bidirectional channel type to a defined unidirectional channel type. This is caused by the following condition in the Go spec:

Assignability

...

  • x is a bidirectional channel value, T is a channel type, x's type V and T have identical element types, and at least one of V or T is not a defined type.

However, it is allowed to first convert to a non-defined type, then to the defined type. If I can convert a defined type V to an intermediate non-defined type, then to a defined type T, then I should be able to convert directly from V to T.

To clarify, the proposal is not to change assignability, but to add a conversion case that allows the conversion of a defined bidirectional channel type to a defined unidirectional channel type, where the element types are identical.

@gopherbot gopherbot added this to the Proposal milestone Mar 5, 2020
@ianlancetaylor ianlancetaylor changed the title Proposal: Allow conversion of defined types of directional channels proposal: Go 2: allow conversion of defined types of directional channels Mar 5, 2020
@ianlancetaylor ianlancetaylor added v2 A language change or incompatible library change LanguageChange labels Mar 5, 2020
@ianlancetaylor
Copy link
Contributor

For language change proposals, please fill out the template at https://go.googlesource.com/proposal/+/refs/heads/master/go2-language-changes.md .

When you are done, please reply to the issue with @gopherbot please remove label WaitingForInfo.

Thanks!

@gopherbot gopherbot added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Mar 5, 2020
@gopherbot
Copy link

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@golang golang locked and limited conversation to collaborators Apr 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

3 participants