Skip to content

Commit e7aa6a1

Browse files
authored
parser, checker: allow lambdas anywhere anonymous functions are expected (#19436)
1 parent 28234c7 commit e7aa6a1

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

vlib/v/checker/lambda_expr.v

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ pub fn (mut c Checker) lambda_expr(mut node ast.LambdaExpr, exp_typ ast.Type) as
77
if node.is_checked {
88
return node.typ
99
}
10-
if !c.inside_fn_arg {
11-
c.error('lambda expressions are allowed only inside function or method callsites',
12-
node.pos)
13-
return ast.void_type
14-
}
1510
if exp_typ == 0 {
1611
c.error('lambda expressions are allowed only in places expecting function callbacks',
1712
node.pos)

vlib/v/parser/expr.v

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -837,10 +837,6 @@ fn (mut p Parser) process_custom_orm_operators() {
837837
}
838838

839839
fn (mut p Parser) lambda_expr() ?ast.LambdaExpr {
840-
if !p.inside_call_args {
841-
return none
842-
}
843-
844840
// a) `f(||expr)` for a callback lambda expression with 0 arguments
845841
// b) `f(|a_1,...,a_n| expr_with_a_1_etc_till_a_n)` for a callback with several arguments
846842
if !(p.tok.kind == .logical_or
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
struct Foo {
2+
sum fn (int, int) int
3+
}
4+
5+
fn test_lambda_as_struct_field_value() {
6+
foo := Foo{
7+
sum: |x, y| x + y
8+
}
9+
assert foo.sum(6, 6) == 12
10+
}

0 commit comments

Comments
 (0)