Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jorgeluismireles opened this issue Feb 7, 2025 · 6 comments
Labels
Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one.

Comments

@jorgeluismireles
Copy link

jorgeluismireles commented Feb 7, 2025

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.

@jorgeluismireles jorgeluismireles added the Bug This tag is applied to issues which reports bugs. label Feb 7, 2025
Copy link

Connected to Huly®: V_0.6-22087

@JalonSolov
Copy link
Contributor

Why do you think this should work? Cols knows nothing about Data. Data does know about Cols, since it implements it.

@jorgeluismireles
Copy link
Author

Workaround:

fn main() {
	mut cols := []Cols{}
	cols << []Data{}
	cols << []Data{}
	print_first_last(cols)
}

But if we already have an array []Data and don't want to push one of one to a []Cols to call the function I wonder if is possible... Thoughs?

@JalonSolov
Copy link
Contributor

Change

pub fn print_first_last(cols []Cols) {

to

pub fn print_first_last(cols []Data) {

@jorgeluismireles
Copy link
Author

print_first_last is a helper to print three rows: the first element, second row with ... and third row the last element:

pub fn print_first_last_table_cols(cols []Cols) {
	len := cols.len
	if len == 0 { // empty array
		println('  empty')
	} else { // first element
		println('  ${0:004d}) ${cols[0].cols()}')
	}
	if len > 2 { // intermediate
		println('        ...')
	}
	if len > 1 { // last element
		println('  ${len:004d}) ${cols[len-1].cols()}')
	}
}

and knows nothing about Data content nor (there are more) implementors which want to use the function with the condition they implements Cols method cols.

Now I realize I need to copy Data elements to Cols, that I think could be prevented to reduce programmer's code.

Having a large array of Data invites to use something like this:

	mut cols := []Cols{ len:data.len } // array of interfaces not initalized
	for i, d in data {
		cols[i] = d
	}
	print_first_last(cols)

which produces:

arrays of interfaces need to be initialized right away, therefore `len:` cannot be used (unless inside `unsafe`, or if you also use `init:`)

unless we add @[heap] to Data.

As i said I think solving somehow the expectation of this issue would help programmer reducing his/her code.

Maybe this issue should be changed from Bug to Feature request

@jorgeluismireles
Copy link
Author

Just wanted to be sure and check that Golang doesn't accept the feature:

package main

type Cols interface {
	Cols() string
}

func PrintFirstLast(cols []Cols) {
	// ...
}

type Data struct {
	// ...
}

func (d Data) Cols() string {
	return "..."
}

func main() {
	data := make([]Data, 3)
	PrintFirstLast(data)
}
./prog.go:22:17: cannot use data (variable of type []Data) as []Cols value in argument to PrintFirstLast

Please remove bug tag from this issue and maybe set to feature request.

@JalonSolov JalonSolov added Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one. and removed Bug This tag is applied to issues which reports bugs. labels Feb 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one.
Projects
None yet
Development

No branches or pull requests

2 participants