diff --git a/doc/docs.md b/doc/docs.md index ecaa454a20..c3df934ef1 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -1064,10 +1064,10 @@ the `it` built-in variable to achieve a classic `map/filter` functional paradigm ```v // using filter, map and negatives array slices a := ['pippo.jpg', '01.bmp', '_v.txt', 'img_02.jpg', 'img_01.JPG'] -res := a.filter(it#[-4..].to_lower() == '.jpg').map(fn (w string) (string, int) { - return w.to_upper(), w.len +res := a.filter(it#[-4..].to_lower() == '.jpg').map(fn (w string) string { + return w.to_upper() }) -// [('PIPPO.JPG', 9), ('IMG_02.JPG', 10), ('IMG_01.JPG', 10)] +// ['PIPPO.JPG', 'IMG_02.JPG', 'IMG_01.JPG'] ``` ### Fixed size arrays diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 24ff4ad7e6..3b25239ef8 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -1707,6 +1707,10 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as else { arg_type } } node.return_type = c.table.find_or_register_array(c.unwrap_generic(ret_type)) + ret_sym := c.table.sym(ret_type) + if ret_sym.kind == .multi_return { + c.error('returning multiple values is not supported in .map() calls', node.pos) + } } else if method_name == 'filter' { // check fn c.check_map_and_filter(false, elem_typ, node) diff --git a/vlib/v/checker/tests/map_func_return_multiple_values_err.out b/vlib/v/checker/tests/map_func_return_multiple_values_err.out new file mode 100644 index 0000000000..357f26af6a --- /dev/null +++ b/vlib/v/checker/tests/map_func_return_multiple_values_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/map_func_return_multiple_values_err.vv:6:17: error: returning multiple values is not supported in .map() calls + 4 | + 5 | fn main() { + 6 | a := [1, 2, 3].map(fun) + | ~~~~~~~~ + 7 | println(a) + 8 | } diff --git a/vlib/v/checker/tests/map_func_return_multiple_values_err.vv b/vlib/v/checker/tests/map_func_return_multiple_values_err.vv new file mode 100644 index 0000000000..d12172f81a --- /dev/null +++ b/vlib/v/checker/tests/map_func_return_multiple_values_err.vv @@ -0,0 +1,8 @@ +fn fun(n int) (int, string) { + return n, n.str() +} + +fn main() { + a := [1, 2, 3].map(fun) + println(a) +}