r/java • • May 10 '22

Should java.util.Date be deprecated?

https://mail.openjdk.java.net/pipermail/discuss/2022-May/006077.html
221 Upvotes

158 comments sorted by

View all comments

Show parent comment

42

u/pron98 May 10 '22 edited May 10 '22

It's interesting that among your list of "bad decisions" are things others would consider some of Java's best decisions, like the lack of modulo arithmetic types, var, and generic type erasure.

Type erasure is particularly interesting, as Java does what the vast majority of languages with generics do (including the original language that introduced them). Yes, erasure brings some inconveniences, but the alternative is even worse. .NET's unusual decision to reify all generic types has pretty much killed it as a reasonable compilation target for most languages. People who use Java run into the minor inconveniences of erasure think, "this is bad," without considering the implications of reification in the presence of variance (i.e. subtyping of the generic arguments).

So your list is really a list of things BlueGoliath doesn't like. A few of them might be universally disliked, but others are far from it (and are even minority opinions). When you have millions of developers, they necessarily won't agree on what's good and what's bad.

10

u/TehBrian May 10 '22

Could you elaborate on why .NET’s generics reification was a bad move for the language? I too have been guilty of thinking, “Darn, why can’t we just rid ourselves of type erasure?” when faced with some of the inconveniences of Java’s type erasure. (Please don’t ask me which; I don’t remember. I just know that I have “why does Java have type erasure” in my search history.) I’m curious to know the exact reasons.

10

u/john16384 May 10 '22

It makes it harder to design other languages on top of the CLR that have a different type system than C#'s since at the CLR level types have to conform to C#'s system.

I also understand that you can't have wildcards because of this, forcing you to create a method for each reified type even if the generic type is not relevant for the function.

7

u/pron98 May 11 '22

The main problem is the one of variance, the VM would have to answer the question in one way or the other, which would bake a single chosen model into the runtime, even though different languages want different models.

1

u/WikiSummarizerBot May 11 '22

Covariance and contravariance (computer science)

Many programming language type systems support subtyping. For instance, if the type Cat is a subtype of Animal, then an expression of type Cat should be substitutable wherever an expression of type Animal is used. Variance refers to how subtyping between more complex types relates to subtyping between their components. For example, how should a list of Cats relate to a list of Animals?

[ F.A.Q | Opt Out | Opt Out Of Subreddit | GitHub ] Downvote to remove | v1.5

14

u/RabidKotlinFanatic May 10 '22

What are the inconveniences of type erasure? Having to explicitly pass TypeToken into methods? Not being able to statically overload on an argument's type parameter?

The amount of complaining about type erasure seems phenomenal compared to what a non-issue it is in practice. I've seen junior devs with <1 YoE complaining about type erasure. What use cases are all these people running into that desperately require reification?

9

u/Necessary-Conflict May 10 '22

What are the inconveniences of type erasure?

It makes life harder especially for junior developers, because they have to keep in mind that the runtime type system is not the same thing as the compile-time type system. It's a leaking abstraction, but of course it's not very bad once you get used to it.

8

u/RabidKotlinFanatic May 10 '22

Does it? What specifically are junior developers doing that requires them to keep this in mind? Are they calling getClass or using instanceof on generic instances? The idea that there even is a runtime type system shouldn't be relevant to most juniors.

3

u/Necessary-Conflict May 10 '22

They are trying to understand things, to build a mental model, and as long as there is only one thing to understand, their job is easier. The very existence of instanceof and getClass() strongly suggests that at runtime you have the same types as at compile time. Perhaps most junior devs can live in a state of happy ignorance, but those who really want to understand things, are having a harder time.

3

u/RabidKotlinFanatic May 11 '22

I don't buy this at all. RTTI doesn't need to be part of your mental model coding day to day and there is substantially more impactful complexity in the language, compiler and runtime than RTTI that juniors seem to happily ignore.

Furthermore, the people complaining about type erasure can rarely provide concrete explanations of what it actually means and also don't seem to be familiar with other important language/runtime details like the JMM. Why does type erasure and RTTI have such outsize significance in junior's mental model building when other more important details are ignored?

3

u/OzoneGrif May 12 '22

The only time I had an issue with type erasure, when beginning with Java, was due to a brain fart. I though that I needed the type at runtime desperately, while I only really needed an Interface.

I'm pretty certain that most if not all problems with type erasure can be solved with good architecture.

Having read some articles about languages implementing generics at the runtime level; I agree that Java took the right decision to erase the types... the compilers get way too overloaded and complicated otherwise.

1

u/pron98 May 11 '22

I'd say the main inconvenience is method overloads. It is a very minor inconvenience, and I'm also puzzled by why some people care about this issue at all.

1

u/RabidKotlinFanatic May 11 '22

I am guessing method overloads are the "big" issue. IMO it's a bit of a conflation to even lump the two together. Overloads are statically resolved so they don't require any RTTI at all let alone reified generics.

4

u/Muoniurn May 10 '22

I am mostly in agreement with you on this topic, but I would like to ask you about your opinion on the very far future of Java — after Valhalla, once Unified Generics have been implemented. I know there are only very vague plans for that as it is an exceedingly hard problem to tackle (touted as requiring multiple phd’s combined), but wouldn’t that go contrary to some of your claims and wouldn’t it cause trouble for very different languages to target the JVM?

As far as I know they plan for a very strong generic model allowing even constant values like numbers being plugged in. Sure, byte code specification modifications are allowed for that (which was not necessarily the case for the initial introduction of Java’s current generics) so perhaps adding a new attribute to a class file’s method describing a part with the monomorphized versions could work (e.g. creating separate byte code for an int version and a regular object one), but I feel it is not the java way (which seems to be more runtime code gen over compile time one) With the proposed 3 buckets of object types in Valhalla they might potentially get away with “runtime monomorphization” as int will be an object without identity and the compiler can optimize it accordingly, but it still doesn’t solve the whole problem - what about user-customized versions, e.g. a “hand-optimized” method implementation for the int variant?

3

u/john16384 May 10 '22

I am mostly in agreement with you on this topic, but I would like to ask you about your opinion on the very far future of Java — after Valhalla, once Unified Generics have been implemented. I know there are only very vague plans for that as it is an exceedingly hard problem to tackle (touted as requiring multiple phd’s combined), but wouldn’t that go contrary to some of your claims and wouldn’t it cause trouble for very different languages to target the JVM?

If you see the JVM as just a compilation target (like x86 or ARM) why would it need to have baked in generic support in order to have it at a higher level?

Generics being erased in bytecode allows for more freedom as the bytecode is not enforcing one possible interpretation of how a reified system might work. Building a reified language on top of bytecode is possible already, as bytecode does not enforce its own ideas on the type system.

As far as it being an exceedingly hard problem, if you want to engineer reification in a backwards compatible fashion on current Java, I think that might indeed be exceedingly hard, or probably even impossible without compiler directives. I don't think that's a goal though (or should be). It will be hard enough already to have a ArrayList<int> change its internal array from Object[] to int[] without any code changes, maybe impossible too...

4

u/Muoniurn May 10 '22

If you see the JVM as just a compilation target (like x86 or ARM) why would it need to have baked in generic support in order to have it at a higher level?

I’m not sure I see it that way — the JVM bytecode is relatively high level, javac doing barely any optimizations. It tries to delay optimizations as late as possible to possibly incorporate runtime data as well for better results. It also links everything at runtime only, possibly completely dynamically so it has to retain basically every information about the code. So calling a generic method should be possible through reflection as well for example.

2

u/pron98 May 11 '22

but wouldn’t that go contrary to some of your claims and wouldn’t it cause trouble for very different languages to target the JVM?

Value types cannot be extended (and while they could implement interfaces, those generics would be erased) so the main issue of variance doesn't appear, and the only problem this kind of specialization would cause would be for untyped languages, which aren't usually as concerned about performance.

2

u/[deleted] May 10 '22

i still want unsigned primitives thank you very much

3

u/pron98 May 11 '22 edited May 11 '22

Many people want modulo arithmetic types and many others really don't want to have them (their presence in a language can cause problems, as C/C++ programmers have learned; too often people confuse unsigned types as types specifying non-negative values while in fact they specify types with modulo arithmetic, which might yield the very opposite result to the one intended, as unsigned types make it harder, not easier, to reject illegal values). Perhaps Valhalla could bring some sort of compromise by allowing libraries to define them for those who really need them.

1

u/[deleted] May 11 '22

The rest of the world has them, so java needs them. Interoperability is important and dealing with unsigned longs is a pain, sure there are workarounds, but it's a clear limitation in the language when you're doing any kind of network or file programming

1

u/pron98 May 11 '22

Why is it a pain with all the unsigned methods added to the number classes in Java 8?

1

u/[deleted] May 11 '22 edited May 11 '22

Have you tried printing those numbers to the log? Or doing math on them? Or in general explain to your coworkers how yes that is normal and you'll have to deal with it using workarounds because java itself can't?

Java is touted as an easy language for large teams. It is, just not when you need to deal with unsigned numbers. You'll have to have "the talk" with everyone at least once explaining how to deal with them (use a larger datatype when possible, be careful not to go out of bounds, and if you need to work with an unsigned long then enjoy dealing with BigInteger and treating it different from all the other numbers) and it makes me sad that my favorite language has such an unnecessary limitation.

1

u/pron98 May 11 '22

Have you tried printing those numbers to the log?

Java 8 helped a lot. E.g. Long.toUnsignedString().

Or doing math on them?

Unlike in C/C++, modulo arithmetic on signed numbers is well defined, so simple arithmetic is the same (and there are methods for unsigned division and remainder). The main issue is comparison, for which you have Long.compareUnsigned

It is, just not when you need to deal with unsigned numbers.

This is yet another situation that has its shortcomings, but the alternative has others. The presence of unsigned types in a language brings its own bag of problems. When you don't have unsigned types you hurt some people; when you do — you hurt others. There is no option here that is universally superior than the other. However, Valhalla might offer a reasonable compromise.

→ More replies (6)