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

cmd/go2go: 2-valued values cannot be assigned to two variables. #40815

Closed
YoshikiShibata opened this issue Aug 15, 2020 · 3 comments
Closed

cmd/go2go: 2-valued values cannot be assigned to two variables. #40815

YoshikiShibata opened this issue Aug 15, 2020 · 3 comments

Comments

@YoshikiShibata
Copy link

@YoshikiShibata YoshikiShibata commented Aug 15, 2020

2-valued values cannot be assigned to two variables: https://go2goplay.golang.org/p/rKe_LDnHgM_m

package main

type Predicate[type T] func(t T) bool

type prevStream[type T] struct {
	req  chan struct{}
	data chan T
}

func (ps prevStream[T]) prevData() (T, bool) {
	ps.req <- struct{}{}
	d, ok := <-ps.data
	return d, ok
}

func (ps prevStream[T]) closePrev() {
	close(ps.req)
}

type genericStream[type T] struct {
	prevStream[T]
	nextReq  chan struct{}
	nextData chan T
}

func (gs *genericStream[T]) filter(predicate Predicate[T]) {
	for range gs.nextReq {
		data, ok := <-gs.prevStream.prevData()
		if !ok {
			close(gs.nextData)
			gs.prevStream.closePrev()
			return
		}

		for !predicate(data) {
			data, ok = <-gs. prevStream.prevData()
			if !ok {
				close(gs.nextData)
				gs.prevStream.closePrev()
				return
			}
		}
		gs.nextData <- data
	}
}

func main() {
}
type checking failed for main
prog.go2:28:17: 2-valued gs.prevStream.prevData() (value of type (T, bool)) where single value is expected
prog.go2:36:17: 2-valued gs.prevStream.prevData() (value of type (T, bool)) where single value is expected
@zephyrtronium
Copy link
Contributor

@zephyrtronium zephyrtronium commented Aug 16, 2020

You're using the receive operator on the result of gs.prevStream.prevData(), which is two values, not a channel. The receive operator cannot be used on two values, hence the type check failure.

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Aug 16, 2020

Agreed. This is not a bug.

@YoshikiShibata
Copy link
Author

@YoshikiShibata YoshikiShibata commented Aug 17, 2020

Oops. Sorry for bothering.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.