From 3327fd23d05c805267dbbb5c6af13752dcc42521 Mon Sep 17 00:00:00 2001 From: Adam Oates <31167933+islonely@users.noreply.github.com> Date: Sun, 24 Sep 2023 04:19:29 +0000 Subject: [PATCH 1/4] parser, checker: allow lambda anywhere fn expected --- vlib/v/checker/lambda_expr.v | 5 ----- vlib/v/parser/expr.v | 4 +--- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/vlib/v/checker/lambda_expr.v b/vlib/v/checker/lambda_expr.v index 20c77fb520e736..246b5d7cb5e609 100644 --- a/vlib/v/checker/lambda_expr.v +++ b/vlib/v/checker/lambda_expr.v @@ -7,11 +7,6 @@ pub fn (mut c Checker) lambda_expr(mut node ast.LambdaExpr, exp_typ ast.Type) as if node.is_checked { return node.typ } - if !c.inside_fn_arg { - c.error('lambda expressions are allowed only inside function or method callsites', - node.pos) - return ast.void_type - } if exp_typ == 0 { c.error('lambda expressions are allowed only in places expecting function callbacks', node.pos) diff --git a/vlib/v/parser/expr.v b/vlib/v/parser/expr.v index 4c93f8773edf76..9645681855e5c1 100644 --- a/vlib/v/parser/expr.v +++ b/vlib/v/parser/expr.v @@ -837,9 +837,7 @@ fn (mut p Parser) process_custom_orm_operators() { } fn (mut p Parser) lambda_expr() ?ast.LambdaExpr { - if !p.inside_call_args { - return none - } + // a) `f(||expr)` for a callback lambda expression with 0 arguments // b) `f(|a_1,...,a_n| expr_with_a_1_etc_till_a_n)` for a callback with several arguments From 3a1c7820f900cc26f262d7a26239e3509a7cd9d8 Mon Sep 17 00:00:00 2001 From: Adam Oates <31167933+islonely@users.noreply.github.com> Date: Sun, 24 Sep 2023 04:42:17 +0000 Subject: [PATCH 2/4] add test --- vlib/v/tests/lambda_as_struct_field_value_test.v | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 vlib/v/tests/lambda_as_struct_field_value_test.v diff --git a/vlib/v/tests/lambda_as_struct_field_value_test.v b/vlib/v/tests/lambda_as_struct_field_value_test.v new file mode 100644 index 00000000000000..c2765c03df57ff --- /dev/null +++ b/vlib/v/tests/lambda_as_struct_field_value_test.v @@ -0,0 +1,10 @@ +struct Foo { + sum fn(int, int) int +} + +fn test_lambda_as_struct_field_value() { + foo := Foo{ + sum: |x, y| x + y + } + assert foo.sum(6, 6) == 12 +} \ No newline at end of file From 2102c46adfb3b0cd70b4be0414e31e09028180ca Mon Sep 17 00:00:00 2001 From: Adam Oates <31167933+islonely@users.noreply.github.com> Date: Mon, 25 Sep 2023 01:36:23 +0000 Subject: [PATCH 3/4] tests: vfmt --- vlib/v/tests/lambda_as_struct_field_value_test.v | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vlib/v/tests/lambda_as_struct_field_value_test.v b/vlib/v/tests/lambda_as_struct_field_value_test.v index c2765c03df57ff..712e438c94c08f 100644 --- a/vlib/v/tests/lambda_as_struct_field_value_test.v +++ b/vlib/v/tests/lambda_as_struct_field_value_test.v @@ -1,5 +1,5 @@ struct Foo { - sum fn(int, int) int + sum fn (int, int) int } fn test_lambda_as_struct_field_value() { @@ -7,4 +7,4 @@ fn test_lambda_as_struct_field_value() { sum: |x, y| x + y } assert foo.sum(6, 6) == 12 -} \ No newline at end of file +} From 194d434346519536913c375ba19b2b9700db16d0 Mon Sep 17 00:00:00 2001 From: Adam Oates <31167933+islonely@users.noreply.github.com> Date: Mon, 25 Sep 2023 01:51:23 +0000 Subject: [PATCH 4/4] parser: vfmt expr.v --- vlib/v/parser/expr.v | 2 -- 1 file changed, 2 deletions(-) diff --git a/vlib/v/parser/expr.v b/vlib/v/parser/expr.v index 9645681855e5c1..ba69499a54dbcb 100644 --- a/vlib/v/parser/expr.v +++ b/vlib/v/parser/expr.v @@ -837,8 +837,6 @@ fn (mut p Parser) process_custom_orm_operators() { } fn (mut p Parser) lambda_expr() ?ast.LambdaExpr { - - // a) `f(||expr)` for a callback lambda expression with 0 arguments // b) `f(|a_1,...,a_n| expr_with_a_1_etc_till_a_n)` for a callback with several arguments if !(p.tok.kind == .logical_or