Skip to content

Commit

Permalink
parser, checker: allow lambdas anywhere anonymous functions are expec…
Browse files Browse the repository at this point in the history
…ted (#19436)
  • Loading branch information
islonely authored Sep 25, 2023
1 parent 28234c7 commit e7aa6a1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
5 changes: 0 additions & 5 deletions vlib/v/checker/lambda_expr.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 0 additions & 4 deletions vlib/v/parser/expr.v
Original file line number Diff line number Diff line change
Expand Up @@ -837,10 +837,6 @@ 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
if !(p.tok.kind == .logical_or
Expand Down
10 changes: 10 additions & 0 deletions vlib/v/tests/lambda_as_struct_field_value_test.v
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit e7aa6a1

Please sign in to comment.