Returning a value as an optional
[Edit: as mentioned here] Returning an int or *int as an optional function result does not compile in the playground:
fn f() ?int {
return 4
}
fn g() ?*int {
i:=5
return &i
}
Returning a struct instance does work.
Then:
fn main() {
mut o := f()? // unexpected token: `?`
mut o := f() // OK
o = 4
i := o or {return} // returns!
println(i)
}}
The first f() call doesn't compile. Then with o = 4 main returns early. Commenting out o = 4 does work.
This looks to be resolved and clarified. Refer to #67 for the explanation.
@Bowero this issue continues on from that issue, read the discussion. Please reopen.
I see. I was too quick with drawing a conclusion. My sincerest apologies.
In the current version of v, this fails to compile, with the following message:
foo.v:6:3: error: expr(): bad token `:=`
4 |
5 | fn g() ?*int {
6 | i:=5
| ~~
7 | return &i
8 | }
It works now, just have to use &
fn g() ?&int {
i:=5
return &i
}