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

parser: implement thread returns result and multi_returns(fix #19281) #20194

Merged
merged 1 commit into from
Dec 16, 2023

Conversation

shove70
Copy link
Contributor

@shove70 shove70 commented Dec 16, 2023

  1. Implement Can't have thread type as result, option, or multi-return. #19281
  2. Add tests.
struct Foo {
mut:
	field1 thread !int
	field2 thread ?int
	field3 thread (int, int)
}

fn Foo.new() Foo {
	mut foo := Foo{
		field1: spawn get_error()
		field2: spawn get_none()
		field3: spawn get_multi_returns()
	}
	return foo
}

fn (mut foo Foo) bar() {
	if _ := foo.field1.wait() {
		assert false
	}
	if _ := foo.field2.wait() {
		assert false
	}
	a, b := foo.field3.wait()
	assert a == 1 && b == 2
}

fn get_error() !int {
	return error('error')
}

fn get_none() ?int {
	return none
}

fn get_multi_returns() (int, int) {
	return 1, 2
}

fn test_main() {
	mut foo := Foo.new()
	foo.bar()
}

@Delta456
Copy link
Member

I don't think that result and multi return type threads should even be allowed.

@shove70
Copy link
Contributor Author

shove70 commented Dec 16, 2023

I don't think that result and multi return type threads should even be allowed.

Well, there may be some reasons I haven't thought of. Can you explain them in detail :)

@Delta456
Copy link
Member

We generally don't allow result types and multi return types as struct fields. I don't see a reason why to have an exception.

@shove70
Copy link
Contributor Author

shove70 commented Dec 16, 2023

We generally don't allow result types and multi return types as struct fields. I don't see a reason why to have an exception.

hmm, I see what you mean.

But I think the struct fields type here is "thread"

@spytheman
Copy link
Member

spytheman commented Dec 16, 2023

Yes, in this case, the fields contain threads. The types after the thread keyword, are the types of what the thread results will be.

Copy link
Member

@spytheman spytheman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work.

@spytheman spytheman merged commit 136193a into vlang:master Dec 16, 2023
54 checks passed
@shove70 shove70 deleted the thread_ret branch December 16, 2023 21:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants