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

tests: add optional_method_err.vv

This commit is contained in:
Delyan Angelov 2020-09-12 13:12:06 +03:00
parent 0801f88d0a
commit 37311883c1
2 changed files with 27 additions and 0 deletions

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/optional_method_err.vv:17:10: error: inc_to_limit() returns an option, but you missed to add an `or {}` block to it
15 | mut a := Abc{}
16 | for _ in 0 .. 4 {
17 | _ := a.inc_to_limit(2)
| ~~~~~~~~~~~~~~~
18 | eprintln('a: $a')
19 | }

View File

@ -0,0 +1,20 @@
struct Abc {
mut:
x int
}
fn (mut a Abc) inc_to_limit(max int) ?int {
if a.x >= max {
return error('x is already $max')
}
a.x++
return a.x
}
fn main() {
mut a := Abc{}
for _ in 0 .. 4 {
_ := a.inc_to_limit(2)
eprintln('a: $a')
}
}