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

Storing a function result value in a variable does not work as expected in an or block #21504

Closed
ttytm opened this issue May 15, 2024 · 0 comments · Fixed by #21717
Closed

Storing a function result value in a variable does not work as expected in an or block #21504

ttytm opened this issue May 15, 2024 · 0 comments · Fixed by #21717
Labels
Bug This tag is applied to issues which reports bugs. Result Type Bugs/feature requests, that are related to `!Type`. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.

Comments

@ttytm
Copy link
Member

ttytm commented May 15, 2024

Describe the bug

Storing the result value of a function call in a variable behaves differently than directly passing the function multiple times.

Reproduction Steps

No variable usage, expected result:

fn str_ret_fn(str string) string {
	return str
}

fn foo(str string) !string {
	if str.contains('foo') {
		return str
	}
	return error('error')
}

var := foo('bar') or {
	// foo_var := str_ret_fn('foo') // uncomment for unexpected behaviour
	if str_ret_fn('foo') == 'foo' {
		foo(str_ret_fn('foo')) or {
			eprintln(err)
			exit(1)
		}
	} else {
		eprintln(err)
		exit(1)
	}
}

println(var) // `foo`

Variable usage, unexpected result:

fn str_ret_fn(str string) string {
	return str
}

fn foo(str string) !string {
	if str.contains('foo') {
		return str
	}
	return error('error')
}

var := foo('bar') or {
	foo_var := str_ret_fn('foo')
	if foo_var == 'foo' {
		foo(foo_var) or {
			eprintln(err)
			exit(1)
		}
	} else {
		eprintln(err)
		exit(1)
	}
}

println(var) // `println(NIL)`

Expected Behavior

No difference weather using a variable or not.

Current Behavior

See repro above.

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.5 58a8fc6

Environment details (OS name and version, etc.)

macos, linux arm64

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@ttytm ttytm added the Bug This tag is applied to issues which reports bugs. label May 15, 2024
@felipensp felipensp added Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Result Type Bugs/feature requests, that are related to `!Type`. labels May 31, 2024
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. Result Type Bugs/feature requests, that are related to `!Type`. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants