v
v copied to clipboard
c error while there is a `or` on the return line
While working on https://github.com/vlang/v/issues/15197 I found another wired bug while I run the following code
gitpod /workspace/v (macros/io.read) $ cat test.v
fn unwrap_int() ?int {
return 1
}
fn unwrap_function() !int {
return unwrap_int() or {error("we are issing the return?")}
}
fn main() {
x := unwrap_int() or {panic(err)}
println(x)
}
Shouldn't
fn unwrap_function() !int {
return unwrap_int() or {error("we are issing the return?")}
}
be this, instead?
fn unwrap_function() !int {
return unwrap_int() or {none}
}
mh!
I think the return type is a result, so or a value (int) or a error, so I make the compiler happy by using the or {return error("...")}
Well... ?int
is optional return, meaning valid value or error. !int
means result return, meaning value value or none
. You have !int
as the return type.
?int
: value of type int
or none
!int
: value of type int
or error
Well... ?int is optional return, meaning valid value or error. !int means result return, meaning value value or none. You have !int as the return type.
I do not know, I just follow the RFC and he said the opposite for ?int
@JalonSolov after the split none
can only be used with ?Foo
, error
with !Foo
.
But the old code using error
with ?Foo
will keep working for 2 years.
Sorry - had it backwards. Too much confusion with which punctuation to use when... :-\