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

checker: add a test for fn call arg of fn pointer mismatch (#15757)

This commit is contained in:
yuyi 2022-09-14 22:46:07 +08:00 committed by GitHub
parent fd1b3fc861
commit ea4152ee14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 0 deletions

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/fn_call_arg_mismatch_err_d.vv:17:26: error: cannot use `fn (string) f64` as `fn (string) ?Flag` in argument 2 to `parse_option`
15 |
16 | fn main() {
17 | t := parse_option('45', parse_percent)?
| ~~~~~~~~~~~~~
18 | println(t)
19 | }

View File

@ -0,0 +1,19 @@
type Flag = bool | f64 | int | string
fn parse_percent(value string) f64 {
f_val := value.f64()
if f_val >= 0 && f_val <= 100 {
return f_val
} else {
return 0
}
}
fn parse_option(str string, validator fn (string) ?Flag) ?Flag {
return validator(str)
}
fn main() {
t := parse_option('45', parse_percent)?
println(t)
}