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

return error() in match #16387

Open
ghost opened this issue Nov 11, 2022 · 3 comments
Open

return error() in match #16387

ghost opened this issue Nov 11, 2022 · 3 comments
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@ghost
Copy link

ghost commented Nov 11, 2022

V version: 0.3.2 993e21e
OS: ArchLinux

What did you do?

fn foo() !{
	s := match true {
		true { 'a' }
		else { return error('') }
	}
	println(s)
}

What did you expect to see?
No error, don't know if this is a bug or not.

What did you see instead?

bug.v:4:10: error: `match` expression requires an expression as the last statement of every branch
    2 |     s := match true {
    3 |         true { 'a' }
    4 |         else { return error('') }
      |                ~~~~~~~~~~~~~~~~
    5 |     }
    6 |     println(s)
@ghost ghost added the Bug This tag is applied to issues which reports bugs. label Nov 11, 2022
@ghost ghost changed the title return in match return error() in match Nov 11, 2022
@JalonSolov
Copy link
Contributor

It is a logical error. Because you're trying to assign to a variable, it expects something assignable to come from every branch in the match.

A return statement doesn't give anything assignable, so you get the error message you saw.

You would need to change the code to something like

fn foo() !{
	s := match true {
		true { 'a' }
		else { '' }
	}

	if s == '' { return error('') }
	println(s)
}

@ghost
Copy link
Author

ghost commented Nov 11, 2022

I know it. This is a matter of design. Will to implement this syntax or not.
If not, this would be a feature request.

Error handling is still in its infancy.
Any technically feasible improvements that will make it more useful should be made proactively.
I do not believe that the status quo is the right way to go.

Thanks to their progressive attitude, the current match statement is so much easier to use than it was two years ago.

It would be great to see the same progressive attitude and improvement with regard to error handling.

Same concept as

fn bar() !string{
	return error('')
}

fn foo() !{
	s := match true {
		true { 'a' }
		else { bar()! }
	}
	println(s)
}

@ghost
Copy link
Author

ghost commented Nov 11, 2022

fn foo() !{
	s := match true {
		true { 'a' }
		else { '' }
	}

	if s == '' { return error('') }
	println(s)
}

One of the problems of this code is when match returns a structure, although I used a string for simplicity in above example.
Nil or null defined struct?
If errors can be returned within match, this can be avoided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs.
Projects
None yet
Development

No branches or pull requests

1 participant