1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

doc: more Result fixes

This commit is contained in:
Alexander Medvednikov 2022-10-24 16:26:19 +03:00
parent 2c9e890c3d
commit 690b2c0b9d
2 changed files with 11 additions and 11 deletions

View File

@ -3548,7 +3548,7 @@ fn main() {
} }
println(user.id) // "10" println(user.id) // "10"
println(user.name) // "Charles" println(user.name) // "Charles"
user2 := repo.find_user_by_id2(10) or { return } user2 := repo.find_user_by_id2(10) or { return }
} }
``` ```
@ -3572,21 +3572,21 @@ user := repo.find_user_by_id(7) or {
} }
``` ```
#### Handling optionals #### Handling optionals/results
There are four ways of handling an optional. The first method is to There are four ways of handling an optional/result. The first method is to
propagate the error: propagate the error:
```v ```v
import net.http import net.http
fn f(url string) ?string { fn f(url string) !string {
resp := http.get(url)? resp := http.get(url)!
return resp.body return resp.body
} }
``` ```
`http.get` returns `?http.Response`. Because `?` follows the call, the `http.get` returns `!http.Response`. Because `!` follows the call, the
error will be propagated to the caller of `f`. When using `?` after a error will be propagated to the caller of `f`. When using `?` after a
function call producing an optional, the enclosing function must return function call producing an optional, the enclosing function must return
an optional as well. If error propagation is used in the `main()` an optional as well. If error propagation is used in the `main()`
@ -3621,11 +3621,11 @@ In case of an error, that value would be assigned instead,
so it must have the same type as the content of the `Option` being handled. so it must have the same type as the content of the `Option` being handled.
```v ```v
fn do_something(s string) ?string { fn do_something(s string) !string {
if s == 'foo' { if s == 'foo' {
return 'foo' return 'foo'
} }
return error('invalid string') // Could be `return none` as well return error('invalid string')
} }
a := do_something('foo') or { 'default' } // a will be 'foo' a := do_something('foo') or { 'default' } // a will be 'foo'
@ -3646,7 +3646,7 @@ if resp := http.get('https://google.com') {
println(err) println(err)
} }
``` ```
Above, `http.get` returns a `?http.Response`. `resp` is only in scope for the first Above, `http.get` returns a `!http.Response`. `resp` is only in scope for the first
`if` branch. `err` is only in scope for the `else` branch. `if` branch. `err` is only in scope for the `else` branch.
@ -4201,7 +4201,7 @@ assert_continues_example.v:3: FAIL: fn main.abc: assert ii == 2
assert_continues_example.v:3: FAIL: fn main.abc: assert ii == 2 assert_continues_example.v:3: FAIL: fn main.abc: assert ii == 2
left value: ii = 3 left value: ii = 3
right value: 2 right value: 2
``` ```
Note: V also supports a command line flag `-assert continues`, which will change the Note: V also supports a command line flag `-assert continues`, which will change the
behaviour of all asserts globally, as if you had tagged every function with `[assert_continues]`. behaviour of all asserts globally, as if you had tagged every function with `[assert_continues]`.

View File

@ -31,7 +31,7 @@ pub mut:
allow_redirect bool = true // whether to allow redirect allow_redirect bool = true // whether to allow redirect
} }
pub fn new_request(method Method, url_ string, data string) !Request { pub fn new_request(method Method, url_ string, data string) ?Request {
url := if method == .get && !url_.contains('?') { url_ + '?' + data } else { url_ } url := if method == .get && !url_.contains('?') { url_ + '?' + data } else { url_ }
// println('new req() method=$method url="$url" dta="$data"') // println('new req() method=$method url="$url" dta="$data"')
return Request{ return Request{