v.parser: allow anonymous function to return a function (#10592)
parent
8650ec6916
commit
2e5ed08558
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue