5 ms·
A sound type system is one that prevents illegal operations. Using a null reference is an illegal operation, and Java doesn't prevent this. Hasn't Java's type s
by andrewflnr 10y ago
A sound type system is one that prevents illegal operations. Using a null reference is an illegal operation, and Java doesn't prevent this. Hasn't Java's type system been unsound all along?
- saghm 10y agoI'm not sure I understand the term "illegal operation" here. I would assume it means "an operation that isn't allowed", but in the case anything that is allowed (e.g. using null) would by definition not be illegal. Can you clarify what you mean by this term?
- 6502nerdface 10y agoDereferencing null is allowed at compile time but disallowed at run time; that's what makes it an illegal operation.
- saghm 10y agoAh, so the term in this case is just used to mean "something that cases a runtime error"? I think I misunderstood because the comment I was responding to said "using a null reference", which to me doesn't necessarily imply dereferencing it (although that's open to interpretation of course).
- mattnewton 10y agoI think s?he means operations that would cause a runtime type error/exception by "illegal"
- paulddraper 10y agoA sound type system is one that enforces certain runtime behaviors at compile-time. Using a null reference is not a behavior ether the Java or Scala type systems promise to prevent, just as Rust's type system does not promise to prevent panic!. Java's type system is unsound if, say, without casting or other "unsafe" operations, we could have a compile-time Integer that is actually a runtime String.
- andrewflnr 10y agoSo if I'm expecting my variable to contain a String, and I try to do Stringy operations on it, what difference does it make to me if that fails because it actually contained an Integer or actually contained a null? My program is just as meaningless either way. Maybe from a standpoint of what's officially promised by the runtime, it's sound, but that feels to me like moving the goalposts. In terms of useful guarantees, Java's type system is, let's just say, kind of useless, if "unsound" is technically incorrect.
- paulddraper 10y ago(1) I 100% agree that null is a terrible, terrible idea https://www.lucidchart.com/techblog/2015/08/31/the-worst-mistake-of-computer-science/ https://www.lucidchart.com/techblog/2015/08/31/the-worst-mis... (2) It's all relative. Most languages allow lots of invalid operations on data. What if I'm expecting my variable to contain a non-empty list and I try to do non-empty list things to it and fail? Dependently-typed languages like Idris say everyone else is moving the goal posts by accepting this behavior in their type systems.
- aseipp 10y agoIt's not necessarily a matter of "do these things behave the same at runtime on the JVM I ran my program on", it's a matter of semantics -- what these programs mean -- and that actually does have a direct impact on what happens at runtime, if you widen your horizons a little. Focus on the cause, not the effect. It's not really moving the goalposts, considering nobody was talking about dereferencing a `null` String in the first place, least of all, the paper itself. `null` is crucial to their argument, but injecting your own impressions about how "it's nothing new, because I had a runtime error before, take that, dumb dumb academics" is mostly irrelevant posturing, it's not what the paper is about. Imagine suddenly if your setting was changed, and e.g. you were not running Java code on the JVM, but directly compiled to native code. It's very well defined what happens when you try to use a `String` but it's actually a `null`, even at the bare metal level - you get a NullPointerException. Easy. Catch the exception somewhere, or don't, it's all very straightforward. And despite what you said in your post -- to the contrary, a program which dereferences `null` is completely well defined and totally meaningful! It is not nonsense from a semantics point of view. Does a program which dereferences null do anything useful, all on its own? No. But it absolutely is a program that has meaning and you can describe what will happen by looking at it (roughly speaking), because its behavior is fully defined. What happens at the bare-metal level when you try to use this Unsound module to convert an `Integer` to a `String`? Well... Bad things will probably happen, up-to and possibly including things like memory corruption or outright termination, depending on how the implementation works. Maybe there is an optimization so small `Integer`s are represented with a different object layout than a large `String` is, at runtime. The runtime system will likely clobber itself if it tried to directly coerce these two different objects, with different sizes, fields, layouts, etc -- because like most typed languages, it would likely rely on the object type to describe memory layout (for things like the GC, so it knows where to find pointers). Classic memory corruption scenario. The JVM itself is not broken by this problem, so it is "correctly" handled for most practical purposes, but only by that coincidence that the JVM retains type safety here. Despite that, there is still no true "meaning" to this program. It is, like the prior program, useless -- but the prior program was meaningful. This program is not. Similarly, it's necessary to understand these kinds of corner cases if we want to do things like formalize compilers, semantics, and design find better points in the design space that can ameliorate these problems (either by tooling, language revisions/changes, or entirely new languages). For most end users, you're basically right, things like this will "only" manifest as a runtime error, and a runtime error is a runtime error is a runtime error. But the paper is about the actual semantics of Java the language, which is a much more broad topic, as opposed to the fact that yes, there's a new way to get a runtime exception.
- plaguuuuuu 10y agoIllegal operation doesn't seem like an explicit enough definition. I'm not knowledgeable about type systems, but I figured a type system would be sound if there's no way to construct code where the result is undefined.