-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
proposal: spec: allow assignment-compatible values in append and copy #15209
Comments
This will require allocating a slice under the covers. []int and []interface{} do not have the same memory and unless something magic happens, the only way to achieve this is to allocate the []interface slice and run the loop. In other words, except for microoptimizations this must do what the current user-written code does, and involves an allocation and an O(len(slice)) copy. I am not making an argument for or against, just saying something that should appear in the proposal. |
Perhaps confusingly, I've actually made two proposals here. In the first one, only the copy and append primitives are affected, and no slice will need to be allocated under the covers. In the second ("Alternative proposal") one, yes, a new slice would need to be allocated, which I hope I've made a little clearer now. |
The more magic builtins The explicit loop communicates the intent, well, explicitly. And there's room for the compiler to recognize and optimize the hell out of those loops. |
cznic: At this point I think I'm mostly trending towards the second proposal, because it's actually reasonably intuitive that the "..." modifier could (when necessary) assign each element in turn, As for readability, I don't think it would make for less readable code, but perhaps you're seeing |
Code requiring awareness of more features packed to an "overloaded" function is less readable in my books. I should have perhaps said comprehensible instead, to be more accurate. |
I believe the name |
I really want to like the alternative proposal, but I think it is important that you must explicitly indicate that your are making an allocation. Like Another possibility (is it the right place?) that would fulfill the use case in the document would be to only have
which feels less verbose than the first proposal while still completely explicit. While it cover the usecase in the proposal, its scope is narrower as you cannot append with it. So I think it's important to define what is really important and what is less. What I like about using *and perhaps map/channel, but that's another story. |
From the proposal:
Is an incomplete sentence. And args := append([]string, string(ts…))
strings.Join(args, “ “) should be args := append([]string{}, string(ts...))
strings.Join(args, " ") FWIW, I prefer the first proposal to the alternative proposal. This "looks like Go" to me: args := make([]interface{}, len(xs))
copy(args, xs) In my view, Go generally tries not to allocate a slice without explicit instruction to do so. |
update: I just saw the part about the alternative proposal -- indeed that would involve a slice allocation in the worst case. @robpike (not taking a stance on this proposal), but I don't believe that a slice "allocation" would be necessary as you described. There are two cases:
In the first case, the source is already a slice (thus no allocation is needed). In the second case, the compiler can place an array on the stack (in the above example, a |
/cc @griesemer |
ping @griesemer |
Sorry, just haven't had time to look into this yet. |
There doesn't seem to be a pressing need for this, so leaving for Go 2. |
Normally I interpret "Go 2" to mean language changes that are impossible without breaking backwards compatibility. Has the definition been changed to "non-urgent language changes"? Or did I misunderstand "Go 2"? |
@carlmjohnson What comprises "Go 2" or it's timeframe have not been defined yet. That said, while many agree that this might be a nice expansion on (There has been at least one other proposal in related direction, and we should probably consider all our options before venturing out to a relatively involved change to |
It seems from the examples given that the need that triggered this proposal is array types conversion. But the syntax proposed is awkward. Go already has a type conversion syntax and it already has magic (that does allocation, copy and conversion) to convert func printInts(xs []int) {
fmt.Println([]interface{}(xs)...)
}
func allReader(fs []*os.File) io.Reader {
return io.MultiReader([]io.Reader(rs)...)
} However I'm not endorsing this proposal. Instead, I am against it as it hides a costly conversion (that requires allocation, loop, conversion of each array element) under a very short syntax that people will abuse. Currently code for array conversions is verbose because this is costly at runtime, and I'm fine with that. |
One thing implicit in the proposal, but not explicitly mentioned in the examples (not sure why), is that with the proposed change to append, the following snippet (copied from the proposal):
could actually be shortened even more, to an append-based snippet (instead of the copy-based one shown in the proposal):
Also, regarding append doing "extra allocations" and "O(n) complexity", I don't really undrestand the concern: AFAIK, both copy & append are already expected to do an extra allocation (of the new array; possibly amortized in case of append), and have O(n) complexity (both of them are by definition a shorthand for a loop). Also, for me personally, the above proposed pattern seems short enough, and at the same time implicit enough (the loop and the allocation are all clearly visible by virtue of using append, and the target type is also clearly listed), that I don't see a need for the "alternative proposal". edit: Also, I see it as further advantage of the proposal that it could become the single recommended solution the 3+ "commonly used" variants of the "covariance conversion code pattern", i.e.:
removing the need for choice here and attempts at microoptimization by using len(xs) here and there. And as such, could possibly become a recommended response to #7512, maybe even landing in the FAQ. edit 2: Regarding "implicitness" of the conversion, note that in the above loops (variant b and c), the append already does an implicit conversion of the x from int to interface{}. In my book, keeping the []interface{} phrase closer to the append could make it actually more explicit. |
|
Ouch, right; but the O(n) still stands I believe. |
Is it possible to implement a converting copy via generics, as the draft proposal stands now? I can't think of a way to specify that one generic type is convertible to another: func Convert[T someConstraint, F otherConstraint](f F) T {
return T(f) // Is it possible to write a constraint to allow this?
} If this function was possible to write, a converting copy could be implemented somewhere in the standard library, such as a theoretical |
According to what I know, there is not a simple way to define a constraint to represent "values of F may be converted to T" by the current generic draft. |
In the current design draft, that is correct. See https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-type-parameters.md#no-way-to-express-convertibility . |
@DeedleFake, I examined |
Now that we have generics there's a new way to break this down. The one thing generics can't do is express convertibility so add a builtin Next add two functions to package Using the two together provides the necessary functionality but they are each useful on their own. |
As discussed here, it might be useful to make the append and copy built-ins somewhat more accepting of different types.
I have made a proposal document here:
https://docs.google.com/document/d/1HilKzERLb521XaG0Lgi6zkkj25VlXqc1oHEUmQ1v3aI
The text was updated successfully, but these errors were encountered: