4 ms·
>It was also very different from languages like TypeScript and Java, where if User implemented Named, as well as being able to pass User to a method accepting N
by nemothekid 7y ago
>It was also very different from languages like TypeScript and Java, where if User implemented Named, as well as being able to pass User to a method accepting Named:
This is possible in Java? Is this a new feature? Or is this only true for arrays (rather than List)?
I could have sworn List<Cat> would be incompatible with List<Animal>.
- tsimionescu 7y agoIn Java, Cat[] can be passed to a function expecting Animal[]. List<Cat> can't be passed to someone expecting List<Animal>. The first case is allowed by the language, but it is not type-safe - depending on your usage, you may get type errors at runtime (trying to write a Dog to an Animal[] that is actually a Cat[]). Java does also support a safe way of achieving variance - your function can take a List<? extends Animal>, in which case you can pass a List<Cat> to it. The compiler also makes sure that a function that takes a List<? extends Animal> can't write a Dog to that list, so this is actually type safe.
- deleted 7y ago[deleted]