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: Go 2: handle type conversion of interface-slices similar to interfaces #38753
Comments
What is now in |
|
But it isn't, I just assigned something that doesn't have a Or are you suggesting that the cast make a copy of the slice's backing store? |
I'd assume it'd have the same logic as: var rc io.ReadCloser
var r io.Reader = rc
Pretty much I'd expect this to happen under the hood, with compiler protection against invalid conversations of course. |
I'd say the OP should have tagged this as |
That works fine because the assignment at line 2 makes a copy. The value contained in The situation with slices is more like this:
Now what does To be problematic, you need to be able to write an |
@OneOfOne Those FAQs apply to Go 2 as well. They explain why the language is as it is, and there is no reason to think that would change in Go 2. (Though I agree that this is a Go 2 proposal.) |
That is a very good point, hadn't thought about that. I guess it could be solved by panicking in this case instead of throwing a compiler error, but I'm not sure it's better. |
Java throws |
What you want is See also #38385 |
Based on the discussion above, this is a likely decline. Leaving open for four weeks for final comments. |
I would like to echo @networkimprov and suggest limiting this proposal to
Currently, this does not compile and instead requires a loop. |
cc @griesemer for thoughts re |
Right now In general, other than Still a likely decline. Leaving open for another week. -- for @golang/proposal-review |
Alternatively, the compiler can recognize the loop and convert it to the equivalent of |
In general converting from one interface type to another requires changing the value to use a different method table. It can't be implemented as |
I think that the arguments against this (hasty) proposal are good. Feel free to close the issue :) |
No change in consensus. |
Abstract
Make type conversion of interface slices behave like conversion of the underlying interfaces. This would for example simplify the use of variadic functions that take interfaces.
The argument can also be made for map and array types, but it's especially nice for slices.
Background
When trying to create a "MultiReadCloser", as a wrapper over
io.MultiReader
I encountered a compilation error, something like this:Playground: https://play.golang.org/p/R8xY0d1aTWO
This is a bit confusing, since a single interface can be converted, but not the whole slice.
It is even more confusing due to the fact that a new slice of the desired interface has to be created for this to work, which seems like a fairly expensive operation, compared to just passing the pointer. But, maybe it's an expensive operation for the compiler as well, I don't know.
Proposal
Allow type conversion for slices containing interfaces, e.g. from
[]io.ReaderCloser
to[]io.Reader
or[]io.Closer
, just as this would be allowed for the interfaces. I think this would be very convenient, specially in variadic functions.Rationale
See the background section and if that doesn't convince you, I formally make the 🦆-typing argument: if a group of ducks quack they are ducks.
The text was updated successfully, but these errors were encountered: