v
v copied to clipboard
Option type comparisons are permitted by V compiler.
Describe the bug
fn main() {
a := ?string("hi")
if a == "hi" { // C ERROR
println("match")
}
}
Expected Behavior
A V error, not a C error.
Current Behavior
Invalid C is generated:
VV_LOCAL_SYMBOL void main__main(void) {
_option_string _t1;
_option_ok(&(string[]) { _SLIT("hi") }, (_option*)(&_t1), sizeof(string));
_option_string a = _t1;
if (_option_string__eq(/*opt*/(*(string*)a.data), _SLIT("hi"))) {
println(_SLIT("match"));
}
}
C error:
error: implicit declaration of function '_option_string__eq' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
if (_option_string__eq(/*opt*/(*(string*)a.data), _SLIT("hi"))) {
^
1 warning and 1 error generated.
Reproduction Steps
See description
Possible Solution
No response
Additional Information/Context
No response
V version
V 0.3.3 ee4150f
Environment details (OS name and version, etc.)
MacOS
I am not sure if comparison operators should work on options.
I thought the plan was to eventually allow this:
if a {
assert a == "hi" // smartcast: a is a string
}
So maybe, the correct code (when the above works) should be this?
if a {
if a == "hi" {
println("match")
}
}
For the records, currently you can do this:
if a != none { }
After discussion on discord, updated description:
- This should not be allowed by the V compiler.
aneeds upwrapping (to a string) before comparison.- Should be a V error