What are the benefits of Java's types erasure?

后端 未结 11 1695
夕颜
夕颜 2020-11-28 01:45

I read a tweet today that said:

It\'s funny when Java users complain about type erasure, which is the only thing Java got right, while ignoring all th

11条回答
  •  半阙折子戏
    2020-11-28 02:14

    A subsequent post by the same user in the same conversation:

    new T is a broken program. It is isomorphic to the claim "all propositions are true." I am not big into this.

    (This was in response to a statement by another user, namely that "it seems in some situations 'new T' would be better", the idea being that new T() is impossible due to type erasure. (This is debatable — even if T were available at runtime, it could be an abstract class or interface, or it could be Void, or it could lack a no-arg constructor, or its no-arg constructor could be private (e.g., because it's supposed to be a singleton class), or its no-arg constructor could specify a checked exception that the generic method does not catch or specify — but that was the premise. Regardless, it's true that without erasure you could at least write T.class.newInstance(), which handles those issues.))

    This view, that types are isomorphic to propositions, suggests that the user has a background in formal type theory. (S)he very likely does not like "dynamic types" or "runtime-types" and would prefer a Java without downcasts and instanceof and reflection and so on. (Think of a language like Standard ML, which has a very rich (static) type system and whose dynamic semantics do not depend on any type information whatsoever.)

    It's worth keeping in mind, by the way, that the user is trolling: while (s)he likely sincerely prefers (statically) typed languages, (s)he is not sincerely trying to persuade others of that view. Rather, the main purpose of the original tweet was to mock those who disagree, and after some of those disagree-ers chimed in, the user posted follow-up tweets such as "the reason java has type erasure is that Wadler et al know what they are doing, unlike users of java". Unfortunately, this makes it hard to find out what (s)he's actually thinking; but fortunately, it also likely means that it's not very important to do so. People with actual depth to their views don't generally resort to trolls that are quite this content-free.

提交回复
热议问题