mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
v.parser: allow anonymous function to return a function (#10592)
This commit is contained in:
@@ -648,7 +648,8 @@ fn (mut p Parser) anon_fn() ast.AnonFn {
|
|||||||
mut return_type_pos := p.tok.position()
|
mut return_type_pos := p.tok.position()
|
||||||
// lpar: multiple return types
|
// lpar: multiple return types
|
||||||
if same_line {
|
if same_line {
|
||||||
if p.tok.kind.is_start_of_type() {
|
if (p.tok.kind.is_start_of_type() && (same_line || p.tok.kind != .lsbr))
|
||||||
|
|| (same_line && p.tok.kind == .key_fn) {
|
||||||
return_type = p.parse_type()
|
return_type = p.parse_type()
|
||||||
return_type_pos = return_type_pos.extend(p.tok.position())
|
return_type_pos = return_type_pos.extend(p.tok.position())
|
||||||
} else if p.tok.kind != .lcbr {
|
} else if p.tok.kind != .lcbr {
|
||||||
|
20
vlib/v/tests/fn_return_fn_test.v
Normal file
20
vlib/v/tests/fn_return_fn_test.v
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
fn make_squarer() fn (int) int {
|
||||||
|
return fn (n int) int {
|
||||||
|
return n * n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_fn_return_fn() {
|
||||||
|
squarer := make_squarer()
|
||||||
|
assert squarer(10) == 100
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_anon_fn_return_fn() {
|
||||||
|
make_doubler := fn () fn (int) int {
|
||||||
|
return fn (n int) int {
|
||||||
|
return n + n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
doubler := make_doubler()
|
||||||
|
assert doubler(10) == 20
|
||||||
|
}
|
Reference in New Issue
Block a user