1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: remove .replace('T', ...)

This commit is contained in:
Delyan Angelov 2020-11-14 10:11:30 +02:00
parent ec3b96924f
commit 023cddb160
2 changed files with 32 additions and 3 deletions

View File

@ -63,7 +63,6 @@ fn (mut g Gen) gen_fn_decl(it ast.FnDecl, skip bool) {
// foo<T>() => foo_int(), foo_string() etc
gen_name := g.typ(g.cur_generic_type)
name += '_' + gen_name
type_name = type_name.replace('T', gen_name)
}
// if g.pref.show_cc && it.is_builtin {
// println(name)
@ -223,8 +222,6 @@ fn (mut g Gen) fn_args(args []table.Param, is_variadic bool) ([]string, []string
// }
if g.cur_generic_type != 0 {
// foo<T>() => foo_int(), foo_string() etc
gen_name := g.typ(g.cur_generic_type)
arg_type_name = arg_type_name.replace('T', gen_name)
}
is_varg := i == args.len - 1 && is_variadic
if is_varg {

View File

@ -0,0 +1,32 @@
struct BuildData { x int }
struct Tensor { x int }
fn new_tensor<T>(data BuildData) Tensor {
println('data: $data')
x := T(data.x)
println(x)
return Tensor{ data.x }
}
fn test_generic_function_returning_type_starting_with_t() {
ft := new_tensor<f64>(x:123)
println(ft)
assert typeof(ft) == 'Tensor'
assert '$ft' == 'Tensor{\n x: 123\n}'
//
it := new_tensor<int>(x:456)
println(it)
assert typeof(it) == 'Tensor'
assert '$it' == 'Tensor{\n x: 456\n}'
}
// the following verifies that returning a generic type T
// works at the same time as returning a type starting with T
fn new_t<T>(o T) T {
x := T(o)
return x
}
fn test_generic_function_returning_t_type() {
f := new_t<f64>(1.23)
i := new_t<int>(456)
assert '$f' == '1.23'
assert '$i' == '456'
}