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 currently uses the same syntax as labels #2

Open
porky11 opened this issue Oct 24, 2022 · 3 comments
Open

Return currently uses the same syntax as labels #2

porky11 opened this issue Oct 24, 2022 · 3 comments

Comments

@porky11
Copy link

porky11 commented Oct 24, 2022

I think, removing the : after return would be less confusing.

@porky11
Copy link
Author

porky11 commented Oct 24, 2022

In order to make this change backwards compatible in cases where return is the last statement, the last statement could be implicitly returned just like in Rust.

@porky11
Copy link
Author

porky11 commented Oct 24, 2022

I just realized it basically already works like this. It's just enforced to call the label before the return value "return".

This is bad for refactoring.

Look at the else_if example. Let's assume, we don't want to return the value directly after jumping to return, but do something afterwards.

For example this:

fn main() -> i32
{
	var x = 33;

	if x == 0
	{
		x = 10;
		goto return;
	}
	else if x == 1
		goto return;
	else if x == 2
	{
		if x == 5
		{
			x = 17;
		}
		else
			goto return;
	}
	else if x == 3
		goto return;
	else
	{
		x = 55;
	}
	
	return:
	var y = x * x;
	x = y - x;
	x
}

It's not possible. Instead you would have to rename every "goto return" to "goto end", also rename label "return" to "end" and add a new label "return" before x.

So being allowed to use "return" as a label for "goto" while enforcing "return" to be the label before the return value at the end, might not be a good idea.

@SLiV9
Copy link
Owner

SLiV9 commented Oct 26, 2022

Yes return is a bit special in that it's a label that also signals the start of the return value. As a style convention I've started putting the return value on the same line as the label (return: x), but not all the code samples do this yet. That at least prevents adding statements between the return: and the return value.

I do think you raise a good point about ease of refactoring. Instead of the three steps you mention, it's just two: adding a new label before the added instructions, and changing all the goto statements. And perhaps there is some value to the invariant "goto return; never executes any further statements with side effects".

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

No branches or pull requests

2 participants