-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Comments
Connected to Huly®: V_0.6-22087 |
Why do you think this should work? |
Workaround: fn main() {
mut cols := []Cols{}
cols << []Data{}
cols << []Data{}
print_first_last(cols)
} But if we already have an array |
Change pub fn print_first_last(cols []Cols) { to pub fn print_first_last(cols []Data) { |
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 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 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:
unless we add 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 |
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)
}
Please remove bug tag from this issue and maybe set to feature request. |
Describe the bug
I think is a bug otherwise I want to know alternatives.
Data
implements interfaceCols
with functioncols() string
fn print_first_last(cols []Cols) { }
[]Data{ len:3 }
is passed toprint_first_last
function but is not accepted.Nor []&Data{} is accepted.
Reproduction Steps
Expected Behavior
[]Data{}
passed toprint_first_last(c [] Cols)
should be accepted sinceData
implementsCols
and array of implementation should be accepted also.Current Behavior
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.
The text was updated successfully, but these errors were encountered: