v
v copied to clipboard
Ternary expression cannot use nested Ifs
Describe the bug
The ternary expression cannot currently use nested if statements.
Expected Behavior
The value of the nested if
statement to be set to the variable.
Current Behavior
error: `if` expression requires an expression as the last statement of every branch
7 | if true {
8 | 1
9 | } else {
| ~~~~
10 | eprintln('Error')
11 | return
Reproduction Steps
var := if true {
0
} else {
if true {
1
} else {
0
}
}
Possible Solution
No response
Additional Information/Context
No response
V version
V 0.3.3 6b20c57.2d51379
Environment details (OS name and version, etc.)
OS: linux, "Arch Linux"
Processor: 16 cpus, 64bit, little endian, AMD Ryzen 9 5900HX with Radeon Graphics
CC version: cc (GCC) 12.2.1 20230111
getwd: /home/user/Workspaces/V/bug-testing
vmodules: /home/user/.vmodules
vroot: /home/user/v
vexe: /home/user/v/v
vexe mtime: 2023-01-30 21:57:54
is vroot writable: true
is vmodules writable: true
V full version: V 0.3.3 6b20c57.2d51379
Git version: git version 2.39.1
Git vroot status: 0.3.3-3-g2d51379f
.git/config present: true
thirdparty/tcc status: thirdparty-linux-amd64 12f392c3
No problem on M1 Mac
OS: macos, macOS, 13.2, 22D49
Processor: 8 cpus, 64bit, little endian, Apple M1
CC version: Apple clang version 14.0.0 (clang-1400.0.29.202)
V full version: V 0.3.3 4747e70.2d51379
thirdparty/tcc status: thirdparty-macos-arm64 173c526e
Your reproduction step has not the same content as the error
Your reproduction step has not the same content as the error
Sorry, I skimmed the code and missed the intent of the post.
I meant @csfore
Right. The code in the Reproduction Steps
section will not produce the error shown.
@danieldaeschle
This should reproduce it
module main
fn main() {
var := if true {
0
} else if false {
1
} else {
eprintln('Error')
return
}
}
It does for me.
I also think this should work. Similar thing was fixed in match
recently... basically the same issue.
It seems like it shouldn't work, because it's trying to assign nothing to a variable essentially but the error message seems like it's erroring for the wrong reason. The variable doesn't technically exist yet though so I'm not sure what it should do.
It should work because it is short-circuiting the program flow such that var
doesn't need to be defined - it is exiting that code flow and returning to the caller.
Ah, alright. Should I rename the issue to something more appropriate then? As nested If statements do technically work. It's just the short-circuiting that doesn't work.
:man_shrugging:
I will be closing the issue because the program compiles successfully for me.
Feel free to open it again if the problem still exists.