4 ms·
It’s not just a random implementation artifact. Since all slices in Go are mutable, it logically wouldn’t make sense to upcast them while keeping the reference.
by codeflo 5y ago
It’s not just a random implementation artifact. Since all slices in Go are mutable, it logically wouldn’t make sense to upcast them while keeping the reference. That would mean the ability to put other objects in the slice. Same for pointers.
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.
- whateveracct 5y agoAh yes that's true it is mutable But even an immutable list cannot be made covariant. Or, say, funcs.
- josefx 5y agoJava fixed the mutability issue for its generics, you can insert Cats and Dogs into a concrete List<Animal>, but you cannot insert them into a List<? extends Animal>.