mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
This commit is contained in:
parent
ff0adba8a9
commit
6ff1c0a0b2
@ -295,7 +295,8 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
|
||||
}
|
||||
}
|
||||
if struct_sym.info.generic_types.len > 0 && struct_sym.info.concrete_types.len == 0
|
||||
&& !node.is_short_syntax && c.table.cur_concrete_types.len != 0 {
|
||||
&& !node.is_short_syntax && c.table.cur_concrete_types.len != 0
|
||||
&& !is_field_zero_struct_init {
|
||||
if node.generic_types.len == 0 {
|
||||
c.error('generic struct init must specify type parameter, e.g. Foo[T]',
|
||||
node.pos)
|
||||
@ -648,8 +649,9 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
|
||||
node.pos)
|
||||
}
|
||||
}
|
||||
if !field.has_default_expr && field.name !in inited_fields && !field.typ.is_ptr()
|
||||
&& !field.typ.has_flag(.option) && c.table.final_sym(field.typ).kind == .struct_ {
|
||||
if !node.has_update_expr && !field.has_default_expr && field.name !in inited_fields
|
||||
&& !field.typ.is_ptr() && !field.typ.has_flag(.option)
|
||||
&& c.table.final_sym(field.typ).kind == .struct_ {
|
||||
mut zero_struct_init := ast.StructInit{
|
||||
pos: node.pos
|
||||
typ: field.typ
|
||||
|
39
vlib/v/tests/generic_struct_init_with_update_expr_test.v
Normal file
39
vlib/v/tests/generic_struct_init_with_update_expr_test.v
Normal file
@ -0,0 +1,39 @@
|
||||
pub struct Time[T] {
|
||||
pub mut:
|
||||
start T
|
||||
end T
|
||||
}
|
||||
|
||||
pub struct Transform[T] {
|
||||
pub mut:
|
||||
time Time[T]
|
||||
before []T
|
||||
after []T
|
||||
}
|
||||
|
||||
pub fn (t Transform[T]) clone() Transform[T] {
|
||||
return Transform[T]{
|
||||
...t
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (t Transform[T]) default() Transform[T] {
|
||||
return Transform[T]{}
|
||||
}
|
||||
|
||||
fn test_generic_struct_init_with_update_expr() {
|
||||
a := Transform[f64]{
|
||||
before: [0.0, 0.0]
|
||||
after: [320.0, 240.0]
|
||||
}
|
||||
|
||||
b := a.clone()
|
||||
println(b)
|
||||
assert b.before == a.before
|
||||
assert b.after == a.after
|
||||
|
||||
c := a.default()
|
||||
println(c)
|
||||
assert c.before.len == 0
|
||||
assert c.after.len == 0
|
||||
}
|
Loading…
Reference in New Issue
Block a user