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

Cannot use closures as a final statement in an if/else block #20414

Closed
zeozeozeo opened this issue Jan 6, 2024 · 1 comment · Fixed by #20461
Closed

Cannot use closures as a final statement in an if/else block #20414

zeozeozeo opened this issue Jan 6, 2024 · 1 comment · Fixed by #20461
Labels
Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one.

Comments

@zeozeozeo
Copy link
Contributor

zeozeozeo commented Jan 6, 2024

V doctor:

V full version: V 0.4.3 5306469.10d738c
OS: windows, Microsoft Windows 10 Pro v19045 64-bit
Processor: 20 cpus, 64bit, little endian, 

getwd: C:\Users\user\Desktop\Code\leaflang
vexe: C:\Users\user\Desktop\Code\v\v.exe
vexe mtime: 2024-01-06 12:34:25

vroot: OK, value: C:\Users\user\Desktop\Code\v
VMODULES: OK, value: C:\Users\user\.vmodules
VTMP: OK, value: C:\Users\user\AppData\Local\Temp\v_0

Git version: git version 2.42.0.windows.1
Git vroot status: weekly.2024.01.test2-7-g10d738c7
.git/config present: true

CC version: Error: 'cc' is not recognized as an internal or external command,

operable program or batch file.


thirdparty/tcc status: thirdparty-windows-amd64 a39eb79b

What did you do?
v -g -o vdbg cmd/v && vdbg test.v

fn main() {
	x := 5
	f := if x == 5 { |x| x - 5 } else { |x| x }
	println(f(x))
}

What did you expect to see?

0 in stdout

What did you see instead?

test.v:3:19: error: a lambda expression was used, but `void` was expected
    1 | fn main() {
    2 |     x := 5
    3 |     f := if x == 5 { |x| x - 5 } else { |x| x }
      |                      ^
    4 |     println(f(x))
    5 | }
test.v:3:19: error: a lambda expression was used, but `none` was expected
    1 | fn main() {
    2 |     x := 5
    3 |     f := if x == 5 { |x| x - 5 } else { |x| x }
      |                      ^
    4 |     println(f(x))
    5 | }
test.v:3:19: error: the final expression in `if` or `match`, must have a value of a non-void type
    1 | fn main() {
    2 |     x := 5
    3 |     f := if x == 5 { |x| x - 5 } else { |x| x }
      |                      ^
    4 |     println(f(x))
    5 | }
test.v:3:4: error: assignment mismatch: 1 variable(s) 0 value(s)
    1 | fn main() {
    2 |     x := 5
    3 |     f := if x == 5 { |x| x - 5 } else { |x| x }
      |       ~~
    4 |     println(f(x))
    5 | }
test.v:3:19: error: a lambda expression was used, but `string` was expected
    1 | fn main() {
    2 |     x := 5
    3 |     f := if x == 5 { |x| x - 5 } else { |x| x }
      |                      ^
    4 |     println(f(x))
    5 | }
test.v:4:10: error: unknown function: f
    2 |     x := 5
    3 |     f := if x == 5 { |x| x - 5 } else { |x| x }
    4 |     println(f(x))
      |             ~~~~
    5 | }
test.v:4:2: error: `println` can not print void expressions
    2 |     x := 5
    3 |     f := if x == 5 { |x| x - 5 } else { |x| x }
    4 |     println(f(x))
      |     ~~~~~~~~~~~~~
    5 | }
If the code of your project is in multiple files, try with `v .` instead of `v test.v`

Example of this expression in Rust: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=60a8125c5277fb69cf22f9272f60aba7

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.

@shove70
Copy link
Contributor

shove70 commented Jan 6, 2024

Please refer to:
#19874

@shove70 shove70 added the Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one. label Jan 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants