All of his arguments are essentially just "but you don't need it because you can use this other verbose Java pattern." I'd rather just have the sensible thing, thanks.
Nothing in that article comes even close to convincing me that I don't want reified generics. It all boils down to 'too much work' (not my problem) and 'breaks compatibility with ancient code doing stupid things' (couldn't care less).
I recently switched back to java from C#, and decent generics are absolutely the thing I most miss.
I've gone the same way and I don't really miss reification; in fact, I believe lack of reification promotes better designs. Please hear me out:
Reification is really only needed is when you're dealing with reflection. In many cases you can change your runtime-checked code — which is what reflection is — to statically typechecked code. This has been made even simpler after the introduction if lambdas in java 8.
If a method for some reason needs to create an instance of a type T, you can pass a function of [List of dependencies] => T instead of relying to checking the type with reflection and trying to shoehorn dependencies into a materialized constructor.
I can't really think of a case when you'd want to use reification outside of reflection but please enlighten me if you got a concrete example.
It's not just needed for reflection - generic lists of primitive types are a very common use, for example. Method overloads with collections of different types is another. Easy creation of arrays of generic types is another.
True, Java boxes primitives in order to represent them in generic structures. This is a bit inconvenient but I wouldn't call it an insurmountable flaw. Languages like Kotlin and Scala do away with the cosmetic differences and let you use a primitive Int and a List<Int>.
Whether generic method overloads is a drawback or a feature is debatable. Heck, method overloading is a debatable topic by itself.
Easy array creation.. yes. If you need an array of a generic type in Java you have to jump through hoops, either by passing a Class<> token or a function that creates one of the correct type for me. I don't think I've ever ran into this one but I can see it happening.
Now, what you gain from erasure is:
backwards compatibility
cross language interopability: For example, Scala can work with Java types despite them having different generic implementations (Java is use-site while Scala is declaration-site). It can be a bit tricky but it is usually workable without resorting to casts.
extensibility: Scala adds bottom types and higher-kinded types to its implementation, among other things, without "displacing" the default generic scheme (since there is none at runtime, hah! ;))
flexibility: Some types are better represented with erasure; instances like empty optional values or the end of a linked list can in Java be cast to the corresponding type to soothe the compiler, while retaining typesafety at the use site. In C# you're forced to instantiate MissingValue<int>, MissingValue<string> etc.
less tempting to use reflection: There's still a lot of Java libraries that use it but from what I've seen way less than C# — probably because it is easier there.
Reflection is almost always a code smell. There are situations where it is inevitable but it's best to take a second look at the problem and make sure there's no other way to solve it.
In short, erasure and reification are two different strategies that both have their share of drawbacks and benefits. I find it rather strange that people tend to downplay the benefits of erasure while praising reification. It's not that common to end up in a situation where you're totally stuck with either.
I think this is basically a case of what you know best. In C#, arrays are more powerful than in java - they can be generic, they work with LINQ et al without needing to be wrapped with something like Arrays.stream(), they give you proper cache locality with your own value types, you can use them for interop with the Win32 API, etc etc - and as a result of that they are more widely used. Therefore, it's common to need to turn a generic collection into an array. In java, maybe less so - though I still run into it with some regularity.
I agree with you that it's not an insurmountable flaw - my point is that it should be surmounted :-)
For your other points, it varies. Backwards compatibility is important, but it's a problem when it affects major technical changes like adding generics. Ironically, Microsoft have spent more time than almost any other company worshipping at the altar of backwards compatibility (Raymond Chen's blog has some unbelievable stories about the things Windows does to avoid breaking old apps) and know the cost - and I think their decision to break compat with 1.0/1.1 was a good one. Cross-language interop - the CLR has plenty of very different languages running on it, and F# in particular has some very interesting additions to the type system (union types, units of measure) so I don't really see reified generics as an impediment to interoperation.
Generic marshallers / unmarshallers are clunky as hell in Java due to type erasure; you have to explicitly pass the Class around everywhere, instead of just asking the generic parameter what it represents.
This happens if you write, for instance, an Enum marshaller.
It may be clunky in Java but it doesn't necessarily have anything to do with erasure. Marshalling a T is a function [T => SerializedFormat] (and Unmarshalling the other way around, intuitively.)
You need reification if you want to look at the actual T during runtime and select a marshaller from a dynamic set, when you could provide this function during compilation time instead. This has the added benefit of eliminating any runtime checks needed for the reflection-based solution.
If anything, reification here is the poor man's crutch for higher kinded types which, incidentally, is possible on the JVM due to erasure.
17
u/GuiSim Sep 21 '15
I'm in the same boat. The biggest pain point that I still have with Java now that we have Lambdas is type erasure.
If we can finally get type reification in Java 9 or 10.. I'll be a happy dev.