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

checker: fix return map index with or_block (fix #20391) #20544

Merged
merged 1 commit into from
Jan 15, 2024

Conversation

yuyi98
Copy link
Member

@yuyi98 yuyi98 commented Jan 15, 2024

This PR fix return map index with or_block (fix #20391).

  • Fix return map index with or_block.
  • Add test.
fn get_value1(m map[string]int, key string) ?int {
	return m[key] or { none }
}

fn get_value2(m map[string]int, key string) !int {
	return m[key] or { error('not found') }
}

fn main() {
	m := {
		'aaa': 111
	}
	if ret1 := get_value1(m, 'aaa') {
		println(ret1)
		assert ret1 == 111
	} else {
		assert false
	}

	if ret2 := get_value2(m, 'aaa') {
		println(ret2)
		assert ret2 == 111
	} else {
		assert false
	}
}

PS D:\Test\v\tt1> v run .
111
111

@spytheman
Copy link
Member

spytheman commented Jan 15, 2024

I can not decide on whether the example should work.

From one point of view:

return expr

should behave exactly the same as:

tmp := expr
return tmp

and you can not do tmp := expr_res or { error('x') },
but you can do: tmp := expr_opt or { none }.

From another point of view, it seems clear what is wanted by just:

fn f() ?Type { return expr or { none } }

and by:

fn f() !Type { return expr or { error('x') } }

@medvednikov, what do you think, should V work like in this PR?

@spytheman
Copy link
Member

(It is excellent work, anyways, whatever is decided.)

@yuyi98
Copy link
Member Author

yuyi98 commented Jan 15, 2024

Here is to omit return, equivalent to return none and return error('....').

@medvednikov
Copy link
Member

I think this should be merged.

@medvednikov medvednikov merged commit 46abcd9 into vlang:master Jan 15, 2024
54 checks passed
@yuyi98 yuyi98 deleted the fix_return_map_value_none branch January 16, 2024 00:50
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.

map access or block behavior is inconsistent with the rest of the language
3 participants