v.parser: allow anonymous function to return a function (#10592)

pull/10597/head
shadowninja55 2021-06-28 04:51:24 -04:00 committed by GitHub
parent 8650ec6916
commit 2e5ed08558
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

@ -648,7 +648,8 @@ fn (mut p Parser) anon_fn() ast.AnonFn {
mut return_type_pos := p.tok.position()
// lpar: multiple return types
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_pos = return_type_pos.extend(p.tok.position())
} else if p.tok.kind != .lcbr {

View 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
}