mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
parent
560afac5d5
commit
ac7e809464
@ -303,6 +303,13 @@ pub fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type {
|
|||||||
c.error('unknown struct: $type_sym.name', node.pos)
|
c.error('unknown struct: $type_sym.name', node.pos)
|
||||||
return ast.void_type
|
return ast.void_type
|
||||||
}
|
}
|
||||||
|
.any {
|
||||||
|
// `T{ foo: 22 }`
|
||||||
|
for mut field in node.fields {
|
||||||
|
field.typ = c.expr(field.expr)
|
||||||
|
field.expected_type = field.typ
|
||||||
|
}
|
||||||
|
}
|
||||||
// string & array are also structs but .kind of string/array
|
// string & array are also structs but .kind of string/array
|
||||||
.struct_, .string, .array, .alias {
|
.struct_, .string, .array, .alias {
|
||||||
mut info := ast.Struct{}
|
mut info := ast.Struct{}
|
||||||
|
25
vlib/v/tests/generics_struct_init_in_generic_fn_test.v
Normal file
25
vlib/v/tests/generics_struct_init_in_generic_fn_test.v
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
module main
|
||||||
|
|
||||||
|
pub struct Person {
|
||||||
|
pub mut:
|
||||||
|
id int
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_generics_struct_init_in_generic_fn() {
|
||||||
|
leo := Person{
|
||||||
|
id: 1
|
||||||
|
}
|
||||||
|
println(leo)
|
||||||
|
assert leo.id == 1
|
||||||
|
|
||||||
|
ret := do(leo)
|
||||||
|
println(ret)
|
||||||
|
assert ret.id == 2
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn do<T>(t T) T {
|
||||||
|
max := T{
|
||||||
|
id: 2
|
||||||
|
}
|
||||||
|
return max
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user