array: [].map(fn...) return type can be different than original type (#7300)
parent
e6f651978e
commit
0aacc9a80a
|
@ -676,6 +676,23 @@ fn test_anon_fn_arg_map() {
|
|||
assert a == [2,3,4]
|
||||
}
|
||||
|
||||
fn test_anon_fn_arg_different_type_map() {
|
||||
i_to_str := fn (i int) string {
|
||||
return i.str()
|
||||
}
|
||||
a := [1, 2, 3].map(i_to_str)
|
||||
|
||||
assert a == ['1', '2', '3']
|
||||
}
|
||||
|
||||
fn test_anon_fn_inline_different_type_map() {
|
||||
a := [1, 2, 3].map(fn (i int) string {
|
||||
return i.str()
|
||||
})
|
||||
|
||||
assert a == ['1', '2', '3']
|
||||
}
|
||||
|
||||
fn test_array_str() {
|
||||
numbers := [1, 2, 3]
|
||||
assert numbers == [1,2,3]
|
||||
|
|
|
@ -1086,9 +1086,8 @@ fn (mut c Checker) check_map_and_filter(is_map bool, elem_typ table.Type, call_e
|
|||
if arg_expr.decl.params.len > 1 {
|
||||
c.error('function needs exactly 1 argument', arg_expr.decl.pos)
|
||||
} else if is_map &&
|
||||
(arg_expr.decl.return_type != elem_typ || arg_expr.decl.params[0].typ != elem_typ) {
|
||||
c.error('type mismatch, should use `fn(a $elem_sym.name) $elem_sym.name {...}`',
|
||||
arg_expr.decl.pos)
|
||||
(arg_expr.decl.return_type == table.void_type || arg_expr.decl.params[0].typ != elem_typ) {
|
||||
c.error('type mismatch, should use `fn(a $elem_sym.name) T {...}`', arg_expr.decl.pos)
|
||||
} else if !is_map &&
|
||||
(arg_expr.decl.return_type != table.bool_type || arg_expr.decl.params[0].typ != elem_typ) {
|
||||
c.error('type mismatch, should use `fn(a $elem_sym.name) bool {...}`',
|
||||
|
@ -1103,8 +1102,9 @@ fn (mut c Checker) check_map_and_filter(is_map bool, elem_typ table.Type, call_e
|
|||
}
|
||||
if func.params.len > 1 {
|
||||
c.error('function needs exactly 1 argument', call_expr.pos)
|
||||
} else if is_map && (func.return_type != elem_typ || func.params[0].typ != elem_typ) {
|
||||
c.error('type mismatch, should use `fn(a $elem_sym.name) $elem_sym.name {...}`',
|
||||
} else if is_map &&
|
||||
(func.return_type == table.void_type || func.params[0].typ != elem_typ) {
|
||||
c.error('type mismatch, should use `fn(a $elem_sym.name) T {...}`',
|
||||
arg_expr.pos)
|
||||
} else if !is_map &&
|
||||
(func.return_type != table.bool_type || func.params[0].typ != elem_typ) {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
vlib/v/checker/tests/array_map_anon_fn_err_b.vv:2:21: error: type mismatch, should use `fn(a int) int {...}`
|
||||
vlib/v/checker/tests/array_map_anon_fn_err_b.vv:2:21: error: type mismatch, should use `fn(a int) T {...}`
|
||||
1 | fn main() {
|
||||
2 | a := [1,2,3,4].map(fn(a string) string { return a })
|
||||
| ~~
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
vlib/v/checker/tests/array_map_anon_fn_err_c.vv:2:21: error: type mismatch, should use `fn(a int) T {...}`
|
||||
1 | fn main() {
|
||||
2 | a := [1,2,3,4].map(fn(a string) {})
|
||||
| ~~
|
||||
3 | println(a)
|
||||
4 | }
|
|
@ -0,0 +1,4 @@
|
|||
fn main() {
|
||||
a := [1,2,3,4].map(fn(a string) {})
|
||||
println(a)
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
vlib/v/checker/tests/array_map_fn_err_b.vv:5:21: error: type mismatch, should use `fn(a int) int {...}`
|
||||
vlib/v/checker/tests/array_map_fn_err_b.vv:5:21: error: type mismatch, should use `fn(a int) T {...}`
|
||||
3 | }
|
||||
4 | fn main() {
|
||||
5 | a := [1,2,3,4].map(add)
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
vlib/v/checker/tests/array_map_fn_err_c.vv:3:21: error: type mismatch, should use `fn(a int) T {...}`
|
||||
1 | fn do_nothing(a string) {}
|
||||
2 | fn main() {
|
||||
3 | a := [1,2,3,4].map(do_nothing)
|
||||
| ~~~~~~~~~~
|
||||
4 | println(a)
|
||||
5 | }
|
|
@ -0,0 +1,5 @@
|
|||
fn do_nothing(a string) {}
|
||||
fn main() {
|
||||
a := [1,2,3,4].map(do_nothing)
|
||||
println(a)
|
||||
}
|
Loading…
Reference in New Issue