5 ms·
It always bothers me that, when discussing why `[]struct` can't be passed to a function expecting `[]interface{}` in Go, people always give the explanation that
by tsimionescu 7y ago
It always bothers me that, when discussing why `[]struct` can't be passed to a function expecting `[]interface{}` in Go, people always give the explanation that it would require extra memory allocation and so on. If it were only that, this would be a fixable problem.
What you should instead mention in these cases is that it would be fundamentally type unsafe to do this. Slices are mutable, so `[]struct` does not fulfill the contract for `[]interface{} `, since I can store any Go type in an `[]interface`{} , but not in an `[]struct`. The same is true for pointers.
Note: if this were only a problem of memory layout, the designers of Go could have built into the language a special type that wraps a slice of structs and behaves like a slice of interfaces, by doing automatic wrapping/unwrapping when you read from/write to an index. This would obviously complicate the runtime, so it may not be desirable anyway, but it could be done and it would solve the problem discussed in the article. In contrast, there is no way to solve the fundemtal type mismatch, so I see it as a much better explanation.
- mkozlows 7y agoYeah, I kept waiting for the article to mention covariance and contravariance, and it never did. A very weird explanation of a not-that-weird phenomenon; I suspect the author didn't really understand what's going on.
- tsimionescu 7y agoTo be fair to the author of the article, the official Go blog, written by Go's designers, offers the same explanation as the article, unfortunately.
- erik_seaberg 7y agoThere was a proposal https://github.com/golang/go/issues/7512 https://github.com/golang/go/issues/7512 to make interface method return types covariant, but it didn't go anywhere, and people seemed to favor the broken behavior (returning a concrete type can fail to satisfy an interface, because the crude matching doesn't care that the type has all the expected methods).
- bogomipz 7y agoMight you or someone else have some links you could share that talk about covariance and contravariance?
- pulisse 7y agoEli Bendersky has a good post[1] on the topic. [1] https://eli.thegreenplace.net/2018/covariance-and-contravariance-in-subtyping/ https://eli.thegreenplace.net/2018/covariance-and-contravari...
- bogomipz 7y agoOh this is great read, thanks!
- js2 7y agoThe wikipedia has a good explanation: https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)#Arrays https://en.wikipedia.org/wiki/Covariance_and_contravariance_...
- sporkland 7y agoMy favorite comment on this topic is from Tim Sweeney: http://lambda-the-ultimate.org/node/735#comment-6678 http://lambda-the-ultimate.org/node/735#comment-6678 Going after two folks in Java and C# communities (the later being involved in developing the language) for committing the same error.
- tsimionescu 7y agoNote that in the meantime, C# has gotten this pretty right, with IEnumerable (a read-only list) and support for co- and contra-variance at type declaration time. Hopefully, some day, Go will also get this 'obscure' feature, though I'm not holding my breath. I would be extremely surprised if their initial version of generics will support anything like this.
- axaxs 7y agoI wonder if this could be caught differently. Such as say, allowing implicitly []struct to be passed where a []interface{} is required, but only if said receiver makes no modification. Otherwise, it's a compile time error. Of course, a read only slice marker would work as well.
- pstuart 7y agoThis one of my top Go complaints as well (and I love the language). If Foo implements interface Bar (Baz() error, Moo() string), then I should be able to pass in []Foo to an argument expecting []Bar, because the former is a slice of a type Bar. The compiler should be a good compiler and do the needful to manage args. All I can expect do on the called side is iterate through the slice, and call the interface function on each element. That sounds contractual to me :-).
- tsimionescu 7y agoAs I explained, that is wrong. You would need a different type, let's call it read-only-slice,to support that. To make the problem more explicit, let's say you have func baz(bars []Bar) { bars[0] = nil } type Foo struct {} baz(make([]Foo, 5)) In this case, the program would fail at runtime, as you can't store a nil in a slice of Foos. The baz() function is perfectly correct in itself, if you passed an array of Bars it would work perfectly.
- pstuart 7y agoI get that part. I want the language/compiler to allow me to use it where it makes sense (as an argument) and prohibit doing things to it that don't (as you demonstrated).
- tsimionescu 7y agoThat would be nice in principle, but where do you expect the error to be thrown in the example above? I don't know of any language which tries to automatically deduce whether a variable is used in a read-only manner or not, and base covariance on this deduction. Even C++, which allows you to explicitly annotate an array to declare that it is read-only (const vector<base*> ) doesn't allow you to pass an array of derived elements instead, so I assume there is some difficulty in implementing these checks and ensuring correctness.
- cjslep 7y agoThe "solution" is to accept an `interface{}` and pass in a `[]struct`! "When writing generic Go, only accept `interface{}` and never a slice" is a painful maxim but works.
- tsimionescu 7y agoWell, that would allow you to call the function, but now you can't write the body of the function without reflection: you can't iterate over an interface{}, and you can't cast a []struct (edit) wrapped in an interface{} (/edit) to a []SomeInterface, so you're back to square one.
- cjslep 7y agoYes, but now you no longer need to change the function declaration and all callsites (major version change) to support new types (if they're slices or not), just need to implement more reflection or typecasting cases within the function (minor version change).
- tsimionescu 7y agoCounterpoint: if you modify the call site to pass in a new value, you don't know whether the function will accept it or not, since you've opted out of any compiler verification.
- cjslep 7y agoThat's a very weak argument since you're already opting out of every other kind of compiler check by using `interface{}` as an element type to begin with. Yet another reason to just use `interface{}`: if the caller already has a value of type `[]Concrete` then they can just pass it in directly; otherwise the caller will need to allocate and copy the value's elements into a new value of `[]interface{}` type since the `[]Concrete` is not castable: https://play.golang.org/p/whh1mAAlN-N https://play.golang.org/p/whh1mAAlN-N
- 7y ago
- fauigerzigerk 7y agoThe problem is that there is no way to pass an immutable slice to a function. What Go really needs is a way to create abstractions that will work with the range loop syntax.
- kissgyorgy 7y agoNot sure what you read or why bother commenting when you didn't but this sentence was in the article: "But slices have mutable indexes. and many functions mutate the slices they're passed."
- simiones 7y agoI read that sentence, but it refers to a different problem. The author is trying to explain why you can't fix the problem by simply having the runtime automatically copy your []struct to a []interface (if it did so, any operation which was supposed to modify the original slice would instead modify the auto-created copy, which wouldn't be correct): > It could allocate a new slice of interface values for you, and assign the concrete User values into corresponding indexes: > [snipping example] > Indeed, for getNames(users) this would actually work fine! But slices have mutable indexes. and many functions mutate the slices they're passed. Again, this is not a real problem, and it could be pretty easily worked around. The real problem is that the slice type is neither covariant nor contravariant.
- coldtea 7y ago>Not sure what you read or why bother commenting when you didn't Not sure why you felt the need to be rude, when you could omit the above phrase, and just say that the article already covers that (and also be a little humble, in case the grandparent already knew that, but doesn't believe it addresses the core issue).
- rwj 7y agoI'm not sure the implementation details can be ignored so easily. With certain exception, the Go authors do not like implicit conversions and the associated runtime costs. I look at the discussion of covariance/contravariance and memory layout as two sides of the same coin.