1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: fix struct field fn type with default value

This commit is contained in:
yuyi 2023-08-10 22:24:03 +08:00
parent 65a493d023
commit 9de852160a
2 changed files with 26 additions and 1 deletions

View File

@ -424,7 +424,7 @@ fn (mut p Parser) parse_type() ast.Type {
is_required_field := p.inside_struct_field_decl && p.tok.kind == .lsbr
&& p.peek_tok.kind == .name && p.peek_tok.lit == 'required'
if p.tok.line_nr > line_nr || p.tok.kind in [.comma, .rpar] || is_required_field {
if p.tok.line_nr > line_nr || p.tok.kind in [.comma, .rpar, .assign] || is_required_field {
mut typ := ast.void_type
if is_option {
typ = typ.set_flag(.option)

View File

@ -0,0 +1,25 @@
struct Flip {
name string = 'NULL'
execute fn () ! = unsafe { nil }
}
fn (flip Flip) exec() ! {
if isnil(flip.execute) {
return
}
println('Executing ${flip.name}')
flip.execute()!
}
fn test_struct_field_default_fn_type_value() {
fl := Flip{
name: 'a function'
execute: fn () ! {
println('Hello, World!')
}
}
fl.exec()!
assert true
}