v icon indicating copy to clipboard operation
v copied to clipboard

Array of variant not accepted for attay of sumtype

Open qtc-de opened this issue 8 months ago • 2 comments

Describe the bug

When having a method that expects an array of a sum type, an array of a variant type is not accepted as input.

Probably not a real bug and related to this issue that basically describes the same behavior for interfaces.

Reproduction Steps

type Animal = Dog | Cat

struct Dog {
	name	string
}

struct Cat {
	name	string
}

fn print_name(animal Animal)
{
	print(animal.name)
}

fn print_names(animals []Animal)
{
	for animal in animals
	{
		print(animal.name)
	}
}

fn main()
{
	cat := Cat { name: "Kitty" }

	// Works fine
	print_name(cat)

	mut cats := []Cat{}
	cats << cat

	// Doesn't work
	print_names(cats)
}

Expected Behavior

I would expect this code snipped to compile fine and to happily use []Cat as []Animal, since Cat is a variant of Animal.

Current Behavior

example.v:32:14: error: cannot use `[]Cat` as `[]Animal` in argument 1 to `print_names`
   30 |     cats << cat
   31 |
   32 |     print_names(cats)
      |                 ~~~~
   33 | }

Possible Solution

Accept variant arrays for sumtype arrays.

Additional Information/Context

No response

V version

V 0.4.10 a9a96b8

Environment details (OS name and version, etc.)

V full version V 0.4.10 e1b9054a27b578a46b1011910b2d632511db8e75.a9a96b8
OS linux

[!NOTE] You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote. Other reactions and those to comments will not be taken into account.

qtc-de avatar Apr 05 '25 12:04 qtc-de

Connected to Huly®: V_0.6-22531

huly-for-github[bot] avatar Apr 05 '25 12:04 huly-for-github[bot]

Well, if you want to see 'Kitty'.

fn main() {
	cat := Cat{ name: 'Kitty' }

	mut cats := []Cat{}
	cats << cat
	print_names(cats[0])	// 'Kitty'
}

Wajinn avatar Apr 07 '25 10:04 Wajinn