Skip to content

Cannot use []Data as []Cols in argument even though Data implements Cols #23666

@jorgeluismireles

Description

@jorgeluismireles

Describe the bug

I think is a bug otherwise I want to know alternatives.

  • Struct Data implements interface Cols with function cols() string
  • There is a function fn print_first_last(cols []Cols) { }
  • An array of []Data{ len:3 } is passed to print_first_last function but is not accepted.

Nor []&Data{} is accepted.

Reproduction Steps

pub interface Cols {
	cols() string
}

pub fn print_first_last(cols []Cols) {
	// ...
}

struct Data implements Cols {
	// data fields
}

fn (c Data) cols() string { // implementation
	return '...'
}

fn main() {
	data := []Data{ len:3 }
	print_first_last(data)
}

Expected Behavior

[]Data{} passed to print_first_last(c [] Cols) should be accepted since Data implements Cols and array of implementation should be accepted also.

Current Behavior

Running code...
Can't run code. The server returned an error:
code.v:19:19: error: cannot use `[]Data` as `[]Cols` in argument 1 to `print_first_last`
   17 | fn main() {
   18 |     data := []Data{ len:3 }
   19 |     print_first_last(data)
      |                      ~~~~
   20 | }
Exited with error status 1
Please try again.

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.9 b91bbad

Environment details (OS name and version, etc.)

https://play.vlang.io/p/d6a82f4ac2

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Feature/Enhancement RequestThis issue is made to request a feature or an enhancement to an existing one.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions