6 ms·
> Ironically, I think a lot of Java's abstraction pain comes from having much more fixed behavior. Fixed behavior isn't necessarily a bad thing. What is always
by catnaroek 10y ago
> Ironically, I think a lot of Java's abstraction pain comes from having much more fixed behavior.
Fixed behavior isn't necessarily a bad thing. What is always a bad thing is when types don't accurately capture what your program really means. Java's own collections documentation says:
“To keep the number of core interfaces small, the interfaces do not attempt to capture such subtle distinctions as mutability, modifiability, and resizability. Instead, certain calls in the core interfaces are optional, enabling implementations to throw an `UnsupportedOperationException` to indicate that they do not support a specified optional operation. Collection implementers must clearly document which optional operations are supported by an implementation.”
https://docs.oracle.com/javase/8/docs/technotes/guides/collections/overview.html https://docs.oracle.com/javase/8/docs/technotes/guides/colle...
In Java, this API design style is the norm rather than the exception, which validates the criticism coming form dynamic language advocates: “You still need to use tests, so why bother with types?” The only way forward is to use types in a way that actually eliminates the need for (some) nontrivial tests.
At the core, the fundamental problems with Java are:
(0) Its type system is too nominal. Every notion, primitive or derived, needs a separate name. The result is that making crisp distinctions takes more effort than programmers are willing to make. In a structurally typed system, a derived notion is just a combination or more basic ones, whether you give the combination a name or not.
(1) It doesn't offer a principled way to express that an operation can naturally have multiple kinds of results. And such operations abound in practice! For example, if you try to get the value associated with a key in a container, the key might or might not be there. Or, if you try to get the i-th element of a list (assume 0-based indexing and nonnegative i for the sake of the example), then i is either a valid index or j positions after the end of the list. None of these possible results is intrinsically “wrong”, but Java can only express operations with one kind of correct result.
- cbsmith 10y ago> Fixed behavior isn't necessarily a bad thing. What is always a bad thing is when types don't accurately capture what your program really means. That's kind of what I meant by having too much fixed behaviour. > In Java, this API design style is the norm rather than the exception, which validates the criticism coming form dynamic language advocates: “You still need to use tests, so why bother with types?” The obvious answer is that while you can't solve everything with types, you can still succinctly represent what would otherwise be a ton of tests with a single type name. > The only way forward is to use types in a way that actually eliminates the need for (some) nontrivial tests. Well, one man's trivial is another man's non-trivial, but I'm happy having a machine automate all my trivial tests. > Its type system is too nominal Yes. It has become more sophisticated over time, but it is still pretty weak, and more importantly the built-in behaviour carries a lot of the consequences of the ridiculously limited nature of the type system in the first place. > It doesn't offer a principled way to express that an operation can naturally have multiple kinds of results. You could make that charge against most of the BCPL languages. C++ gets around it with templates, but even then has all kinds of limitations on how the output type is a function of the input type. > For example, if you try to get the value associated with a key in a container, the key might or might not be there. Java has Optional<T> which is exactly the way to express that case. > None of these possible results is intrinsically “wrong”, but Java can only express operations with one kind of correct result. Exceptions are a reasonable way of expressing alternative results, as is Optional<T>, as is returning a common supertype/interface, as is null (much as I despise it), as is having an object that serves as a discriminated union. That may not all sound great to you, but those are actually very common strategies found in a variety of other languages.
- catnaroek 10y ago> The obvious answer is that while you can't solve everything with types, you can still succinctly represent what would otherwise be a ton of tests with a single type name. Except a name is just a declaration of intentions. A Python class name is just as good a declaration of intentions as a Java class name. But why should I trust an unverified declaration? > Well, one man's trivial is another man's non-trivial, but I'm happy having a machine automate all my trivial tests. Finite discriminate unions are pretty trivial by any reasonable standards, and Java can only handle them by very cumbersome means (visitors), which people who value their time reasonably avoid. > You could make that charge against most of the BCPL languages. C++ gets around it with templates, but even then has all kinds of limitations on how the output type is a function of the input type. Somehow Rust and Swift manage it. Even C++ and D fare better than Java, since finite discriminated unions can be implemented using templates, although pattern matching using variant_visitors is a truly maddening experience. > Java has Optional<T> which is exactly the way to express that case. But `null` still exists, and it's used in several parts of the Java standard library. > Exceptions are a reasonable way of expressing alternative results, as is Optional<T>, as is returning a common supertype/interface, as is null (much as I despise it), as is having an object that serves as a discriminated union. (0) No, exceptions aren't reasonable. They don't always show up in types, so you don't know what kinds of exceptions a particular procedure might throw. (1) Java's `Optional<T>` is hardly more satisfying: how do I simultaneously pattern-match over two or more optionals? (2) In any case, there exist many more interesting discriminated unions than can be expressed using only optionals and recursive types.
- cbsmith 10y ago> A Python class name is just as good a declaration of intentions as a Java class name. Yes, it is just as good a declaration of intentions. > But why should I trust an unverified declaration? Wait a second though... def foo(bar): if !isinstance(bar, Baz): throw WrongTypeException THAT is somewhat equivalent to having: public void foo(Baz bar); The Java one gets verified without test cases (there's literally steps in the process with "Verifier" in the name), but the Python one does not. Most Python code doesn't have all of its parameters laden with type checks, whereas Java code tends to have type names attached to its parameters. More importantly though, that Python type check doesn't actually get verified until runtime, and only on a case by case basis. The static type checks in Java will be done first at compile time, and then again during bind/load time. You still have to write code to verify that you never get a non-Baz "baz" for the Python function, and that's trickier than it looks. > Finite discriminate unions are pretty trivial by any reasonable standards, and Java can only handle them by very cumbersome means (visitors), which people who value their time reasonably avoid. You value your time, so because of cases where you'd like to return more than one type without a common parent type, you would throw out type checking entirely?... > But `null` still exists, and it's used in several parts of the Java standard library. So, Rust at least still has null as well (they say they don't, but in practice they have std::ptr::null and they also have None... now tell me that doesn't create more problems than it solves ;-), as there's this problem that reality is already filled with nulls. In practice, what you are getting at is exactly what I pointed out earlier: Java is locked in to certain behaviours and interfaces that predate the current somewhat more sophisticated state of the type system. > No, exceptions aren't reasonable. They don't show up in types, so you don't know what kinds of exceptions a particular procedure might throw. They do for checked exceptions: final java.sql.ResultSet foo = query.execute(); foo.getInt(1); That's going to get you an int, a SQLException, a RuntimeException, or an Error depending on circumstance. While the Error & RuntimeException aren't checked by the compiler, the SQLException is, and you've got a fairly natural way of writing logic for it too. That's a very reasonable way of declaring that your function will return either an int or a SQLException. Without any test cases, the compiler will tell me if I'm not handling that SQLException case (much to my chagrin). > Java's `Optional<T>` is hardly more satisfying: how do I simultaneously pattern-match over two or more optionals? Generally, I haven't needed to. Usually the optional comes in a different stream or in a different stage of processing a stream. However, for cases where you really have multiple optionals, you typically either pass them as parameters to lambda or create a named type that contains your Optional<T> types. Honestly, given the complexity of those cases, it isn't that great a burden compared to the logic you're going to bind to it... > In any case, there exist many more interesting discriminated unions than can be expressed using only optionals and recursive types. Might be more interesting yes... but for your typical coding context, not that important.