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

find_field: use optional

This commit is contained in:
Alexander Medvednikov 2019-09-18 14:28:11 +03:00
parent d1500511e6
commit d180324413
2 changed files with 22 additions and 15 deletions

View File

@ -1818,7 +1818,7 @@ fn (p mut Parser) dot(str_typ string, method_ph int) string {
// field // field
if has_field { if has_field {
struct_field := if typ.name != 'Option' { p.table.var_cgen_name(field_name) } else { field_name } struct_field := if typ.name != 'Option' { p.table.var_cgen_name(field_name) } else { field_name }
field := p.table.find_field(typ, struct_field) field := p.table.find_field(typ, struct_field) or { panic('field') }
if !field.is_mut && !p.has_immutable_field { if !field.is_mut && !p.has_immutable_field {
p.has_immutable_field = true p.has_immutable_field = true
p.first_immutable_field = field p.first_immutable_field = field
@ -2786,7 +2786,7 @@ fn (p mut Parser) struct_init(typ string) string {
if field in inited_fields { if field in inited_fields {
p.error('already initialized field `$field` in `$t.name`') p.error('already initialized field `$field` in `$t.name`')
} }
f := t.find_field(field) f := t.find_field(field) or { panic('field') }
inited_fields << field inited_fields << field
p.gen_struct_field_init(field) p.gen_struct_field_init(field)
p.check(.colon) p.check(.colon)

View File

@ -400,37 +400,44 @@ fn (table mut Table) add_field(type_name, field_name, field_type string, is_mut
} }
fn (t &Type) has_field(name string) bool { fn (t &Type) has_field(name string) bool {
field := t.find_field(name) _ := t.find_field(name) or { return false }
return (field.name != '') return true
} }
fn (t &Type) has_enum_val(name string) bool { fn (t &Type) has_enum_val(name string) bool {
return name in t.enum_vals return name in t.enum_vals
} }
fn (t &Type) find_field(name string) Var { fn (t &Type) find_field(name string) ?Var {
for field in t.fields { for field in t.fields {
if field.name == name { if field.name == name {
return field return field
} }
} }
//println('ret Var{}') return none
return Var{}
} }
fn (table &Table) type_has_field(typ &Type, name string) bool { fn (table &Table) type_has_field(typ &Type, name string) bool {
field := table.find_field(typ, name) _ := table.find_field(typ, name) or { return false }
return (field.name != '') return true
} }
fn (table &Table) find_field(typ &Type, name string) Var { fn (table &Table) find_field(typ &Type, name string) ?Var {
field := typ.find_field(name) for field in typ.fields {
if field.name.len == 0 && typ.parent.len > 0 { if field.name == name {
parent := table.find_type(typ.parent)
return parent.find_field(name)
}
return field return field
} }
}
if typ.parent != '' {
parent := table.find_type(typ.parent)
for field in parent.fields {
if field.name == name {
return field
}
}
}
return none
}
fn (table mut Table) add_method(type_name string, f Fn) { fn (table mut Table) add_method(type_name string, f Fn) {
if type_name == '' { if type_name == '' {