mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
parent
1305ca662f
commit
170282b2af
@ -557,6 +557,13 @@ pub fn (mut c Checker) infer_fn_generic_types(f ast.Fn, mut call_expr ast.CallEx
|
||||
}
|
||||
} else if param.typ.has_flag(.variadic) {
|
||||
to_set = c.table.mktyp(arg.typ)
|
||||
} else if arg_sym.kind == .struct_ && param.typ.has_flag(.generic) {
|
||||
info := arg_sym.info as ast.Struct
|
||||
generic_names := info.generic_types.map(c.table.get_type_symbol(it).name)
|
||||
if gt_name in generic_names {
|
||||
idx := generic_names.index(gt_name)
|
||||
typ = info.concrete_types[idx]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
19
vlib/v/tests/generic_fn_infer_struct_test.v
Normal file
19
vlib/v/tests/generic_fn_infer_struct_test.v
Normal file
@ -0,0 +1,19 @@
|
||||
struct Node<T> {
|
||||
data T
|
||||
}
|
||||
|
||||
fn foo<T>(n Node<T>) string {
|
||||
return '$n'
|
||||
}
|
||||
|
||||
fn test_generics_fn_infer_struct() {
|
||||
ret1 := foo(Node<int>{})
|
||||
println(ret1)
|
||||
assert ret1.contains('Node<int>{')
|
||||
assert ret1.contains('data: 0')
|
||||
|
||||
ret2 := foo(Node<byte>{})
|
||||
println(ret2)
|
||||
assert ret2.contains('Node<byte>{')
|
||||
assert ret2.contains('data: 0')
|
||||
}
|
Loading…
Reference in New Issue
Block a user