mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
fix optional struct field assignment
This commit is contained in:
parent
54707ff81d
commit
560d1383d1
@ -1441,7 +1441,7 @@ fn ($v.name mut $v.typ) ${p.cur_fn.name}(...) {
|
||||
expr := p.cgen.cur_line[pos..]
|
||||
left := p.cgen.cur_line[..pos]
|
||||
typ := expr_type.replace('Option_', '')
|
||||
p.cgen.resetln(left + 'opt_ok($expr, sizeof($typ))')
|
||||
p.cgen.resetln(left + 'opt_ok(($typ[]){ $expr }, sizeof($typ))')
|
||||
}
|
||||
else if expr_type.starts_with('Option_') &&
|
||||
p.assigned_type == expr_type['Option_'.len..] && p.tok == .key_orelse
|
||||
|
@ -112,3 +112,15 @@ fn test_field_or() {
|
||||
}
|
||||
assert mytitle == 'default'
|
||||
}
|
||||
|
||||
struct Thing {
|
||||
mut:
|
||||
opt ?int
|
||||
}
|
||||
|
||||
fn test_opt_field() {
|
||||
mut t := Thing{}
|
||||
t.opt = 5
|
||||
val := t.opt or { return }
|
||||
assert val == 5
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user