mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
default struct vals
This commit is contained in:
@ -131,7 +131,9 @@ fn (p mut Parser) struct_decl() {
|
||||
}
|
||||
|
||||
mut did_gen_something := false
|
||||
mut i := -1
|
||||
for p.tok != .rcbr {
|
||||
i++
|
||||
if p.tok == .key_pub {
|
||||
if is_pub_field {
|
||||
p.error('structs can only have one `pub:`, all public fields have to be grouped')
|
||||
@ -205,6 +207,15 @@ fn (p mut Parser) struct_decl() {
|
||||
if is_atomic {
|
||||
p.next()
|
||||
}
|
||||
// `a int = 4`
|
||||
if p.tok == .assign {
|
||||
p.next()
|
||||
def_val_type, expr := p.tmp_expr()
|
||||
if def_val_type != field_type {
|
||||
p.error('expected `$field_type` but got `$def_val_type`')
|
||||
}
|
||||
p.table.add_default_val(i, typ.name, expr)
|
||||
}
|
||||
// [ATTR]
|
||||
mut attr := ''
|
||||
if p.tok == .lsbr {
|
||||
|
@ -105,6 +105,7 @@ mut:
|
||||
is_c bool // `C.FILE`
|
||||
enum_vals []string
|
||||
gen_types []string
|
||||
default_vals []string // `struct Foo { bar int = 2 }`
|
||||
// This field is used for types that are not defined yet but are known to exist.
|
||||
// It allows having things like `fn (f Foo) bar()` before `Foo` is defined.
|
||||
// This information is needed in the first pass.
|
||||
@ -441,6 +442,12 @@ fn (table mut Table) add_field(type_name, field_name, field_type string, is_mut
|
||||
table.typesmap[type_name] = t
|
||||
}
|
||||
|
||||
fn (table mut Table) add_default_val(idx int, type_name, val_expr string) {
|
||||
mut t := table.typesmap[type_name]
|
||||
t.default_vals[idx] = val_expr
|
||||
table.typesmap[type_name] = t
|
||||
}
|
||||
|
||||
fn (t &Type) has_field(name string) bool {
|
||||
_ = t.find_field(name) or { return false }
|
||||
return true
|
||||
|
Reference in New Issue
Block a user