38 points · by rbanffy · 5y ago · 52 comments
Any thought put into what that might mean for the soundness of the type system trivially leads to the 'correct' conclusions.
I'm not sure the headline is 'deserved' (it insinuates more alarm than I think is warranted), and the actual paper linked __definitely__ is engaging in some unwarranted hyperbole. "The existential crisis of null"?
The behavior used to get that to happen, as far as I remember, uses the java spec giving a concrete way on how to parse generics. Multiple other compilers (including the official Java 9 one, if I'm reading correctly) actually emitted an error on it, despite "technically" it being "valid" Java code, to emphasize how weird the situation is.
edit: this is of similar (mostly academic) significance to, say, finding a case where you can double-free in Rust without any unsafe code. Sure, rust is unsafe if you use "unsafe", but other than that, you should hope it's sound. It's not at all as catastrophic as JVMs are already pretty much just dynamically typed though, but it's still a bad thing to have.
Still called 'heap corruption'. Even if as far as the JVM is concerned, that's just a list of whatevers - it doesn't matter, on-use the bytecode will cast.
I am Java programmer, actually like java and took no offense on it.
I honestly can't fathom how that works, I thought aversion to verbosity is near-universal among programmers, so how can somebody _like_ (not just use or be okay with) a language that requires you to state the maddeningly obvious and do a mountain of paperwork all the time, so many times and for the simplest most trivial tasks. It's the modern day COBOL and worse.
When I first saw Java coming from C++ and Python object models (among others, but those two I'm most familiar with), I felt the same rage upon seeing that the accessibility modifier must be put before every field or method as people feel when they have to jump through stupid bureaucratic hoops. C++ just requires you to put "['public'|'protected'|'private'] :" then every field or method coming after that colon will be of the accessibility before it, which is the commonsense thing to do because usually all publics are together and all privates are together. It's something so very trivial and so utterly lowbar that C++ managed to do it right on the first try, I don't know what frame of mind was playing out in the mind of the first person who thought that this common thing needs to be multiplied out and stated for every field and every method it annotates.
This was the archtypical model of my interaction with Java ever since, everytime I hear the name I instantly think of the stupid and meaningless convoluted procedures typical of corrupt governments and bloated administration systems. "veryLongTypeName veryLongObjectName = new veryLongTypeName" everywhere as far as the eye can see, obscuring the dance of algorithms and datastructures expressing the core logic in favor of trivialities about allocations and declarations, then designers introduce the useless syntax sugar "var" and gloat about how that enhances readability in some cases, as if the language's syntax shouldn't have been designed like so and trimmed much much more than that from the very beginning. "Everything is a class" because we want to be so cool like our idol objective-C's idol smalltalk, so you have to do stupid things like naming a class "fooUtils" to signal it's just a namespace of static methods, but actually forget that not everything is a class and generics can't be used with primitves because reasons, nevermind the extremly obvious and painfully trivial solution of having the compiler generate a generic_primitiveType class for every generic it encounters, even if it's still a quick-and-dirty hack and only works with code with source available it would be infinitely better and more clean than the hack job they did with autoboxing and unboxing.
Etc, etc, the language is so comically badly-designed it's baffling, I don't think they could have done a worse job if they tried.
I like being able to immediately look at a line, and know what type of variable it declares, and, separately, look at what the variable definition does. When reading code top-to-bottom, you can pretty much ignore variable types, but when scanning code for what you want, you pretty much ignore code, and look only at variable types.
Having to wrap your static utilities in a class does seem a bit pointless, but the alternatives are having a whole another system for outside-of-any-class static methods (which complicates the language model and makes moving parts of your program around harder), or having 'namespace YourUtils { … }', which isn't all that different from a class anyways.
Generics are by far the thing I like least about Java. And that's represented by how they were added to Java - bolted on, not actually properly designed in. A problem with autogenerated types is that that'd generate a new type for each compiled jar file, and including jar files would then have the same type defined multiple times. As far as I understand, being able to trivially fall back to non-generic types was also a reason for the weirdness. (yes, these are horrible reasons, and have resulted in a vastly worse language. Unfortunately the designers valued keeping old things working over making future things better. Yes, Java could easily be better. But it still manages to be good enough for people.)
Generics were bolted on in Java 5, and some painful compromises had to be made to have both backward compatibility and generics. The whole type erasure thing being the prime example.
There is some kind of heap corruption in that you can force a Duck in a list of Cars. But the very first time you try to use that Duck as a Car, the JVM will catch it and hit you with an exception. You won't get C-like undefined behavior this way.
Java had some painfull bugs with real world consequences. This is not one of them.
Now if you want to really abuse Java, try this one :
Field field = Integer.class.getDeclaredField("value");
field.setAccessible(true);
field.set(1, 666);
List<Integer> list = Arrays.asList(1, 2, 3);
System.out.println(list); //prints [666,2,3]
and java allows half of the number 1s in your program to magically change into a 666. Total mayhem ensues. Solution: Don't do this.I would argue that the real problem with Java's rollout of generics is at the language level. The language itself introduced some strange gaps in the semantics of generics, such as failing to build any decent bridges between arrays and generic collections. I don't know that this was necessary. Scala and Kotlin are working out OK, and they have unified arrays with generics. And the other problem is that generic types are not exactly first-class types in Java, even before type erasure takes place. This means handling some key use cases for generics requires resorting to the "super type token" pattern, which is awkward and still not all that well known.
I don't think I'm alone in this sentiment. Coders at Work has an interview with a developer who worked on Java 5, and he also expresses a belief that, in retrospect, the problem with Java generics wasn't type erasure, it's that they rushed the feature and some of the language-level design characteristics weren't fully baked yet. (He didn't go much into specifics, though, so I can't say if our opinions agree in the specifics.)
You're both right and wrong.
You're technically correct, which we all know is the best kind of correct, but you're wrong because you're leaving out very important Gestalt context. (EDIT: Actually, you vaguely touch on it, but you focus on arrays and Lists, which I think misses the point)
You're correct because Rust and Haskell, et al, have fully erased types (well... Rust has trait objects, which are implemented with vtables- does that count?), and nobody really complains about it. So, in a very specifically-literal sense, you're right that it's not the type-erasure that is the (sole) problem in Java.
The real problem is that Java is BIG on *runtime reflection*. You can't "reflect" on types that don't exist, so every time you do use reflection (which is approximately 100% of Java projects) and generics (which is also approximately 100% of Java projects), you're in for nonsense. Between generics, reflection, and null, I literally feel my blood pressure rise when I think about dealing with Java, and even Kotlin.
Where Java leans on runtime reflection, Haskell and Rust cannot and do not. And, therefore, the communities have found compile-time (or just tedious write-time) solutions to things like (de)serialization.
So, type erasure isn't the problem, and runtime reflection isn't a problem. But you can't do both and expect anything good to come of it.
It was necessary. Java made the mistake of letting primitive arrays be (incorrectly) covariant in their type param. Java's implementation of generics is, fortunately, correct in how it handles type parameter variance. However, this means that they are incompatible with Java's primitive arrays. I don't think there is a way to "bridge" them seamlessly without making generics unsound.
So, for example, at run time an Option<i32> ends up just storing one word for indicating whether it's Some or None, and another for the value when it's Some, but doesn't store any information to indicate that those 8 bytes represent an Option<T> of any kind, let alone exactly what kind of value is being stored in it. None of that is needed at run time, because the compiler was able to statically verify that no function is going to try to read them as a u64 instead.
1) Is this likely to happen by accident?
2) Does this enable some functionality not otherwise available (even if it's still a bad idea)
Looking at the paper, it seems like neither of these is the case for Java/Scala types. Given that, I would categorize this as a weird quirk of Java and Scala, and leave it at that.
Gunax has the soul of a mathematician. Confronted with an imperfection, the question is how to eradicate this blight from the world and get us one step closer to perfection, no matter the cost.
Both are equally correct reactions, of course. Just nice to see them in the same place.
This is no longer possible out of the box after "JEP 403: Strongly Encapsulate JDK Internals" [1].
UPDATE: I see there is a flag --illegal-access=permit so if you want to abuse Java, you can still go ahead ;-)
But you can use "--add-opens=java.base/java.lang=ALL-UNNAMED" instead if you really want.
If you really want to shoot yourself in the foot. You can always go native...
The trick is to make the int literal to get boxed so that it hits the cache - which requires a printf (rather than println).
And yea, don't do this.
So I guess the answer here would be "yes" to both parts. Well done!
Well, I'm not sure I redefined 4 specifically, but I tried out redefining a few integer literals.
java.lang.reflect.InaccessibleObjectException: Unable to make field private final int java.lang.Integer.value accessible: module java.base does not "opens java.lang" to unnamed module
It's the reason why the transition from Java 8 to 11 has been painful.
In making it into a List<Integer>, it goes through boxing.
https://docs.oracle.com/javase/tutorial/java/data/autoboxing...
The "half" means that this doesn't impact things like "int foo = 1 + 1" which will be 2 rather than 1,332. It's not because it's half of the uses of 1, but rather that some portion of the code will be converting int to Integer and that will impact those uses.
Scala 3 is one of the best-designed languages I've ever used.
I came into learning it without Scala 2 experience, and not a big fan of hardcore FP (a-la Haskell), and it's intuitive to write as you would Python or Typescript if you choose to.
First/foremost, the community essentially held my hand, especially during the early weeks. I would ask questions on their Discord and always someone would answer them and explain concepts if needed:
They have a great community. The experienced users tend to be heavily FP-oriented but they're respectful of the "I'm not really the Haskell type." sentiment.
I went through the Scala 3 "concepts" book on the website, and then started to build things + ask questions about best practices or how to do stuff on Discord:
https://docs.scala-lang.org/scala3/book/introduction.html
They have also updated the free online courses "Effective Scala" and the Scala specialization to Scala 3:
[1] https://www.scala-lang.org/blog/2016/02/03/essence-of-scala....
Same problem would appear in Haskell, where you can define a term of any type (using a divergent function); the actual value is never executed (because of laziness), so it would crash at runtime. In OCaml, the type system would accept this, but it wouldn't crash (because the "any type" term cannot actually be created, but would result in an exception and/or divergence).
The correct solution is for the type system to track "computability" and require that all terms used in proofs are actually well-defined. Liquid Haskell in particular had this problem that they had to solve by enforcing strictness for such terms.
(Also, technically generic functions in Java/Scala aren't "parametrically polymorphic" - which means that you cannot do any operation on a value of polymorphic type - not even `typeof`).
"Inferred type 'B' for type parameter 'B' is not within its bound; should extend 'U'"