mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
handle empty config structs
This commit is contained in:
parent
2897bac549
commit
f55646746c
@ -903,7 +903,7 @@ fn (p mut Parser) factor() string {
|
||||
return p.map_init()
|
||||
}
|
||||
peek2 := p.tokens[p.token_idx + 1]
|
||||
if p.peek() == .name && peek2.tok == .colon {
|
||||
if p.peek() == .rcbr || (p.peek() == .name && peek2.tok == .colon) {
|
||||
if !p.expected_type.ends_with('Config') {
|
||||
p.error('short struct initialization syntax only works with structs that end with `Config`')
|
||||
}
|
||||
|
@ -493,10 +493,19 @@ fn (p mut Parser) gen_struct_init(typ string, t &Type) bool {
|
||||
if typ == 'tm' {
|
||||
p.cgen.lines[p.cgen.lines.len - 1] = ''
|
||||
}
|
||||
mut is_config := false
|
||||
if p.tok != .lcbr {
|
||||
p.next()
|
||||
} else {
|
||||
is_config = true
|
||||
}
|
||||
p.check(.lcbr)
|
||||
// Handle empty config ({})
|
||||
if is_config && p.tok == .rcbr {
|
||||
p.check(.rcbr)
|
||||
p.gen('($typ) {}')
|
||||
return true
|
||||
}
|
||||
ptr := typ.contains('*')
|
||||
// `user := User{foo:bar}` => `User user = (User){ .foo = bar}`
|
||||
if !ptr {
|
||||
|
@ -194,3 +194,18 @@ fn test_fixed_field() {
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
struct Config {
|
||||
n int
|
||||
def int = 10
|
||||
}
|
||||
|
||||
fn foo_config(c Config) {
|
||||
}
|
||||
|
||||
fn test_config() {
|
||||
foo_config({n: 10, def: 20})
|
||||
foo_config({})
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user