by whateveracct · 4y ago · 0 comments
In theory, a slice of structs that implement an interface should be able to be used as a slice of that interface. But due to the implementation, that requires a full copy.
You can see it this way: there is implicit syntactic sugar that creates an interface value (pointer + type info) when you assign a value of type T to a variable of type interface I where T implements I. This happens on variable assignment, function parameter bindings and return value bindings.
A slice of Is is just a very different type from a slice of Ts. The assignment magic sugar works, but it works at the level of slice elements, not the whole slice.
That’s a basic fact of type safety that people often get wrong. For example, the JVM famously allows upcasting arrays, with very surprising results.
But even an immutable list cannot be made covariant. Or, say, funcs.
It absolutely should not even if it could (which it can not since the memory layouts don’t match), as that undermines the type system.
Yeah but immutability’s not even remotely a thing in go land.