News for hackers. Stuff that matters.
Re:State of Valhalla by MBCook on Thursday December 23, 2021 @11:32PM (#29670226)
As the article says, it was an unfortunate side effect of trying to maintain compatibility.

I imagine C# was able to learn from what was going on with Java at the time and not have to suffer the same fate. Or maybe it was just less popular and could force the issue (I’ve never dealt with it so I don’t know the history well).

Re:State of Valhalla by kgeist on Friday December 24, 2021 @12:05AM (#29670448)
The main use case of generics is collections, and if my memory serves me right, in .NET they simply created a new collections library (System.Collections.Generics) leaving the original intact (System.Collections) allowing old programs to work as before. It lacked 100% compatibility because you couldn't freely interchange the classes (without writing adapters) but from what I gathered, it was a small price to pay compared to type erasure (in my opinion) which prevented more aggressive runtime optimizations/evolution. Today you usually find the old collections in ancient software which hasn't been updated for years.
Re:State of Valhalla by kaba0 on Friday December 24, 2021 @02:21AM (#29671212)
As it is usually brought up, not erasing types also comes with some potential cons: namely making CLR languages dependent on C#’s chosen variance model.

In Java, List<Cat> may or may not be the subclass of List<Animal>, but it is up to the List implementor. This way Scala/Kotlin/another JVM language is free to define their own variance model independent of the host language. C# did limit their language ecosystem with it quite a bit. (Afaik Scala for CLR stopped in part due to this).

Re:State of Valhalla by jayd16 on Friday December 24, 2021 @02:47PM (#29677083)
In C# I believe you can optionally mark generic classes/methods as covariant or contravariant. Is that not enough or does it not get exposed in the CLR or something?

https://docs.microsoft.com/en-us/dotnet/csharp/language-refe... https://docs.microsoft.com/en-us/dotnet/csharp/language-refe...

Re:State of Valhalla by kgeist on Friday December 24, 2021 @10:25AM (#29674324)
How do two languages with two different variance models interoperate in JVM if they share the same type but expect different behavior? Is it safe to share a list created in one language and pass into another if their variance models differ? Having an explicit variance model makes cross-language interoperability safer and easier (which was one of the main selling points of the Common Language Runtime), doesn't it?
Re:State of Valhalla by kaba0 on Friday December 24, 2021 @12:09PM (#29675291)
Simply from an empirical point of view, the CLR is pretty much a desert compared to the flourishing JVM one so while it can be attributed to many things, I am really sure that explicit variance is not that attractive for language developers.
Re:State of Valhalla by lokedhs on Friday December 24, 2021 @05:38AM (#29672432)
List<Cat> cannot be a subclass of List<Animal> as that would imply I can do list.add(dog) into that list.

You probably know this, but I'm replying in the hope it clarifies things to other readers. The way it's expressed in Java is List<? extends Animal>

Re:State of Valhalla by kaba0 on Friday December 24, 2021 @07:05AM (#29672924)
Yeah, java can change the variance model used by generics, but my point is that it is a language-level feature, not something fundamental at a JVM level, which is imo the correct decision.

Also, unfortunately arrays are covariant so Cat[] is a subclass of Animal[] (both in Java and C# actually), where your mentioned example indeed introduces a “poisoned” value, waiting for a classcastexception.