V: Docs do not mention default values in `or` blocks

Created on 10 Jul 2020  路  9Comments  路  Source: vlang/v

V version: V 0.1.28 e47ad33
OS:

What did you do?

fn do_something(n i8) ?i8 {
    if n == 33 {
        return n
    }
    return error("Number doesn\'t exist")
}

x := do_something(1) or {
    print(err)
    return
}
println(x) // Execution stops here?!
println('Continue executing..')

What did you expect to see?

Number doesn't exist
Continue executing..

What did you see instead?
Number doesn't exist

Documentation

Most helpful comment

It's basically a default value that will be used if the optional function returns an error or none:

fn do_something(s string) ?string {
      if s == 'foo' { return 'foo' }
      return error('invalid string') // Could be `return none` as well
}

a := do_something('foo') or { 'default' } // 'foo'
b := do_something('bar') or { 'default' } // 'default'

The expression has to be the same type as the function's return type for this reason.

If, instead of providing a default value and using it throughout the rest of the function, you want to exit early, that's when you can use return.

All 9 comments

In my understanding this program works correctly: the return in the or block returns from the implicit main() function, which terminates the program.

This is more clearly seen when you envelop the code in a function like this:

fn test() {
x := do_something(1) or {
print(err)
return
}
println(x) // Execution stops here?!
println('Continue executing..')
}

test()
println('\nAfter test')

The output is what you expect:

Number doesn't exist
After test

You're right but the problem here is that return is mandatory so how is this handling the error? if return will always terminate the program then what is the purpose of "catching" the error anyway?

Good point.

I think the reason why return here is mandatory is because V combines Option and Result into one type. Unlike Rust for example which has Some and None and then they're paired with pattern matching.

the problem here is that return is mandatory

It isn't, you can add an expression as the final element of the or block, and that will be used as the variable's value instead of returning:

fn do_something(n i8) ?i8 {
    if n == 33 { return n }
    return error("Number doesn't exist")
}

x := do_something(1) or {
    println('ERROR: $err')
    100
}
println('x = $x') // 100
println('Continue executing..')

The docs need to be updated to mention this, though; since the current optionals example has a comment saying "or block must end with return, break, or continue".

It isn't, you can add an expression as the final element of the or block, and that will be used as the variable's value instead of returning:

Nice, but still this expression is mandatory! the result will always be

ERROR: Number doesn't exist
100
Continue executing..

What does this 100 represent ? I tried to add none instead but it hit me with a C error

It's basically a default value that will be used if the optional function returns an error or none:

fn do_something(s string) ?string {
      if s == 'foo' { return 'foo' }
      return error('invalid string') // Could be `return none` as well
}

a := do_something('foo') or { 'default' } // 'foo'
b := do_something('bar') or { 'default' } // 'default'

The expression has to be the same type as the function's return type for this reason.

If, instead of providing a default value and using it throughout the rest of the function, you want to exit early, that's when you can use return.

It's basically a default value that will be used if the optional function returns an error or none:

I did this

b := do_something('bar') or {
    err
}

And it makes more sense :) thank you for clarifying that

No problem, happy to help ;)

I'll keep this open until the docs explain default values as well.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

XVilka picture XVilka  路  3Comments

arg2das picture arg2das  路  3Comments

PavelVozenilek picture PavelVozenilek  路  3Comments

radare picture radare  路  3Comments

lobotony picture lobotony  路  3Comments