diff --git a/vlib/v/tests/generics_return_inconsistent_types_generics_struct_test.v b/vlib/v/tests/generics_return_inconsistent_types_generics_struct_test.v new file mode 100644 index 0000000000..7bbf47b7ce --- /dev/null +++ b/vlib/v/tests/generics_return_inconsistent_types_generics_struct_test.v @@ -0,0 +1,97 @@ +// test generics function that return generics struct +pub struct Optional { +mut: + value T + some bool + typ string +} + +pub fn new_some(value T, b B) Optional { + return { + value: value + some: true + typ: typeof(b).name + } +} + +pub fn some(opt Optional) bool { + return opt.some +} + +pub fn get_value(opt Optional) T { + return opt.value +} + +pub fn get_typ(opt Optional) string { + return opt.typ +} + +pub fn set(mut opt Optional, value T, b B) { + opt.value = value + opt.some = true + opt.typ = typeof(b).name +} + +fn test_inconsistent_types_generics_fn_return_generics_struct() { + mut o := new_some(23, 'aaa') + println(some(o)) + assert some(o) == true + + println(get_value(o)) + assert get_value(o) == 23 + + set(mut o, 42, 'aaa') + println(get_value(o)) + assert get_value(o) == 42 + + println(get_typ(o)) + assert get_typ(o) == 'string' +} + +// test generics method that return generics struct +pub struct Foo { + foo int +} + +pub fn (f Foo) new_some(value T, b B) Optional { + return { + value: value + some: true + typ: typeof(b).name + } +} + +pub fn (f Foo) some(opt Optional) bool { + return opt.some +} + +pub fn (f Foo) get_value(opt Optional) T { + return opt.value +} + +pub fn (f Foo) get_typ(opt Optional) string { + return opt.typ +} + +pub fn (f Foo) set(mut opt Optional, value T, b B) { + opt.value = value + opt.some = true + opt.typ = typeof(b).name +} + +fn test_inconstent_types_generics_method_return_generics_struct() { + foo := Foo{} + mut o := foo.new_some(23, 'aaa') + println(foo.some(o)) + assert foo.some(o) == true + + println(foo.get_value(o)) + assert foo.get_value(o) == 23 + + foo.set(mut o, 42, 'aaa') + println(foo.get_value(o)) + assert foo.get_value(o) == 42 + + println(foo.get_typ(o)) + assert foo.get_typ(o) == 'string' +}