Skip to content

language: support explicit conversion from []A to []B if A is assignable to B #3756

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

Closed
Sajmani opened this issue Jun 19, 2012 · 5 comments
Closed
Labels
FrozenDueToAge LanguageChange Suggested changes to the Go language

Comments

@Sajmani
Copy link
Contributor

Sajmani commented Jun 19, 2012

A slice []A is not assignable to []B, even if A is assignable to B, for reasons
discussed here:
  https://groups.google.com/forum/#!searchin/golang-nuts/slice$20assign|sort:relevance/golang-nuts/Il-tO1xtAyE/jw0-loqeAdEJ

It's easy enough to convert []A to []B with a few lines of code, of course.  But this
friction gets in the way of writing functions that operate on slices of types that
implement common interfaces.

I propose Go support the syntax ([]B)([]A) (or []B([]A), if possible) to automate this
conversion.  This conversion is explicit, not implicit, so it's clear in the program
that this expansion is happening.

For example, consider this function:
// Join joins the elements of a using sep, formatting each element of a as %v would.
func Join(sep string, a ...interface{}) string

If the caller has []time.Time, he or she must first convert this to []interface{} to
call this function.  With this conversion syntax, the caller can write:
  times := []time.Time{ t1, t2, t3 }
  s := Join(",", ([]interface{})(times)...)
@cznic
Copy link
Contributor

cznic commented Jun 20, 2012

Comment 1:

I don't feel comfortable with the idea to allow "conversion" of []non-interface to
[]interface. IMO, it's technically not a conversion when the result and source of the
"conversion" are slices with different memory layouts. It's hidden code invoked by a
conversion like syntax, which I don't like to see in Go.

@Sajmani
Copy link
Contributor Author

Sajmani commented Aug 16, 2012

Comment 2:

Thinking about alternatives, the following would work for my Join example:
// Join joins the elements yielded by next using sep, formatting each element of a as %v
would.
func Join(sep string, next func() (interface{}, bool)) string
times := []time.Time{ t1, t2, t3 }
var i int
s := Join(",", func() (interface{}, bool) {
  if i < len(times) {
    t := times[i]
    i++
    return t, true
  }
  return nil, false
})
Or, if we assume indexed access:
func Join(sep string, at func(i int) (interface{}, bool)) string
times := []time.Time{ t1, t2, t3 }
s := Join(",", func(i int) (interface{}, bool) {
  if i < len(times) {
    return times[i], true
  }
  return nil, false
})
This is probably more efficient than what I proposed in this issue, since it avoids
allocating a new slice.

@rsc
Copy link
Contributor

rsc commented Aug 31, 2012

Comment 3:

I don't believe the conversion suggested here will happen, because it would hide an
unbounded amount of allocation, while all current conversions are bounded.
I will leave this open for someday if you think we should keep the conversation going.

Labels changed: added priority-someday, languagechange, removed priority-triage.

Status changed to LongTerm.

@Sajmani
Copy link
Contributor Author

Sajmani commented Sep 6, 2012

Comment 4:

I'm ok with closing this out.  The callback signatures I suggested in Comment #2 are
more efficient anyway.

@rsc
Copy link
Contributor

rsc commented Sep 10, 2012

Comment 5:

Status changed to WorkingAsIntended.

@Sajmani Sajmani added workingasintended LanguageChange Suggested changes to the Go language labels Sep 10, 2012
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Suggested changes to the Go language
Projects
None yet
Development

No branches or pull requests

4 participants