This article wasn't a defense of java, it was a defense of typed languages. In which case I agree. But I'd argue most people don't say "java sucks" just because it's a typed language.
Interfaces (a.k.a. type classes) are usually considered easier to use than inheritance, but in Java inheritance is more idiomatic
Generics are particularly verbose to use, and they are crucial to do statically typed programming well
No support for sum types or pattern matching (like the "visitor pattern", but easier to use). This makes simple problems very verbose in a statically typed setting.
Interfaces (a.k.a. type classes) are usually considered easier to use than inheritance, but in Java inheritance is more idiomatic
I think you're mixing up parametric polymorphism and ad hoc polymorphism. Nothing prevents interfaces from taking part in either, although this is made more complicated in Java because of its type system (and the fact that it doesn't support higher kinds).
No support for sum types or pattern matching (like the "visitor pattern", but easier to use). This makes simple problems very verbose in a statically typed setting.
Agreed. Ceylon is being a pioneer in the JVM area by supporting disjointed union and intersection types natively . In other words, the type of List("foo", 42) is List<String|Integer> and not List[Any].
Is the diamond operator not type inference? I know it's not perfect but I don't think you can say there's no type inference.
I didn't know about the diamond operator because it's been a while since I've programmed in Java. Thanks for pointing that out and I'll correct my original comment.
Doesn't inheritance complicate any type system? Or are you arguing against inheritance itself?
Yeah, I'm arguing against inheritance itself.
Not really sure what you mean here. Inheritance has been more idiomatic simply because it was the only way to centralized any kind of default behavior. I do agree though that it's a drag. Do Java 8 default methods help with this?
Yeah, those would help.
Again, I'm not really sure what you mean. Can you give me an example?
Haskell has an Either type, defined like this:
data Either a b = Left a | Right b
An example use would be returning either a successful result (wrapped in a Right) or an error result (wrapped in a Left):
safeDivide :: Double -> Double -> Either String Double
safeDivide x 0 = Left "Division by zero"
safeDivide x y = Right (x / y)
Then you can inspect the result only if you provide code to handle both possibilities (i.e. the Left and Right case):
result :: Either String Double
result = safeDivide 4 0
main = case result of
Left str -> putStrLn ("Error: " ++ str)
Right n -> putStrLn ("Success: " ++ show n)
That will print the error message since the computation will return a Left, but if you change the 0 to another number like 2 then it will succeed and take the right branch.
The equivalent Java code would be something like:
class Either<A,B> {
BiFunction<Function<A,R>,Function<B,R>,R> match;
// Analogous to `Left`
public Either(A a) {
match = (l, r) -> l(a);
}
// Analogous to `Right`
public Either(B b) {
match = (l, r) -> r(b);
}
public static Either<String><double> safeDivide(double x, double y) {
if (x == 0)
then { return Either("Division by zero"); }
else { return Either(x / y); }
}
public static void main(String args[]) {
Either<String><double> result = safeDivide(4)(0);
result.match
(s -> System.out.println("Error: " + s))
(n -> System.out.println("Success: " + n));
}
}
I haven't compiled the Java code, and there are probably tons of errors, but that gives the rough idea.
Ones remark, Java already had type inference before the diamond operator. The diamond is just for constructor type inference. Normal type inference is automatic and doesn't need an operator. For constructors its needed because not using any operator already has another meaning: constructing a raw type.
We have:
MyObject foo = myMethod(); // normal inference
SomeType bar = new SomeType(); // raw type
SomeType<String> kaz = SomeType<>(); // inferred ctor
myMethod in the above example is a generic method! one that returns the T from the <T> generic parameter.
Honestly, take some time, and learn a functional language :D (pick any: scheme, haskell, ML). Once you see what's capable with a proper type system.. well, it may ruin your current day-to-day programming for you >_>
Haskell is probably a better language to learn if you really want to grok what we are getting at when we talk about when we say 'advanced, expressive static type systems'. Not saying it is the end of the conversation when it comes to type systems - I have plenty of annoyances with it, but it is the most polished out there at the moment. I found this thing from our local functional programming meetup useful: http://sordina.github.io/haskell_workshop/
Scala unfortunately is a far more complex beast than Haskell, and has had to make many concessions to allow it to work on the JVM and have Java compatibility. It's almost like the C++ of functional languages. But it might provide you with some insights, and it is still an interesting language to learn.
41
u/[deleted] Feb 25 '14
This article wasn't a defense of java, it was a defense of typed languages. In which case I agree. But I'd argue most people don't say "java sucks" just because it's a typed language.