diff --git a/vlib/v/gen/fn.v b/vlib/v/gen/fn.v index 193cbc8297..8ab567c8f0 100644 --- a/vlib/v/gen/fn.v +++ b/vlib/v/gen/fn.v @@ -63,7 +63,6 @@ fn (mut g Gen) gen_fn_decl(it ast.FnDecl, skip bool) { // foo() => 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() => 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 { diff --git a/vlib/v/tests/generic_fn_returning_type_with_T_test.v b/vlib/v/tests/generic_fn_returning_type_with_T_test.v new file mode 100644 index 0000000000..0721c606fe --- /dev/null +++ b/vlib/v/tests/generic_fn_returning_type_with_T_test.v @@ -0,0 +1,32 @@ +struct BuildData { x int } +struct Tensor { x int } +fn new_tensor(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(x:123) + println(ft) + assert typeof(ft) == 'Tensor' + assert '$ft' == 'Tensor{\n x: 123\n}' + // + it := new_tensor(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(o T) T { + x := T(o) + return x +} +fn test_generic_function_returning_t_type() { + f := new_t(1.23) + i := new_t(456) + assert '$f' == '1.23' + assert '$i' == '456' +}