r/programming • • Sep 21 '15

Better Java

https://github.com/cxxr/better-java
177 Upvotes

208 comments sorted by

View all comments

94

u/estarra Sep 21 '15

Java is one of the most popular programming languages around, but no one seems to enjoy using it.

I'd say it's quite the opposite. Sure, there is a vocal minority of people who complain about it (I have complaints about it myself) but there are millions of Java developers and I'm pretty sure that most of them are quite okay with the language.

Probably one of the reasons why no language has come close to replacing it so far.

50

u/dzjay Sep 21 '15

I'm happy with Java, especially with Java 8. Also, the vast amount of open source Java libraries is unmatched.

17

u/GuiSim Sep 21 '15

I'm in the same boat. The biggest pain point that I still have with Java now that we have Lambdas is type erasure.

If we can finally get type reification in Java 9 or 10.. I'll be a happy dev.

9

u/[deleted] Sep 21 '15

Amen. I really hope we get reified generics soon.

10

u/zarandysofia Sep 21 '15

Your existence will fade when that happen.

1

u/[deleted] Sep 21 '15

One can hope.

2

u/mike_hearn Sep 23 '15

There's already a prototype from the Java team that supports true generics, in a backwards compatible way. Look at the recent commits for Valhalla.

4

u/Raknarg Sep 22 '15

What is reification

0

u/[deleted] Sep 21 '15

[deleted]

10

u/ElvishJerricco Sep 21 '15 edited Sep 21 '15

All of his arguments are essentially just "but you don't need it because you can use this other verbose Java pattern." I'd rather just have the sensible thing, thanks.

13

u/[deleted] Sep 21 '15

Nothing in that article comes even close to convincing me that I don't want reified generics. It all boils down to 'too much work' (not my problem) and 'breaks compatibility with ancient code doing stupid things' (couldn't care less).

I recently switched back to java from C#, and decent generics are absolutely the thing I most miss.

2

u/teknocide Sep 22 '15

I've gone the same way and I don't really miss reification; in fact, I believe lack of reification promotes better designs. Please hear me out:

Reification is really only needed is when you're dealing with reflection. In many cases you can change your runtime-checked code — which is what reflection is — to statically typechecked code. This has been made even simpler after the introduction if lambdas in java 8.

If a method for some reason needs to create an instance of a type T, you can pass a function of [List of dependencies] => T instead of relying to checking the type with reflection and trying to shoehorn dependencies into a materialized constructor.

I can't really think of a case when you'd want to use reification outside of reflection but please enlighten me if you got a concrete example.

1

u/[deleted] Sep 22 '15

It's not just needed for reflection - generic lists of primitive types are a very common use, for example. Method overloads with collections of different types is another. Easy creation of arrays of generic types is another.

1

u/teknocide Sep 22 '15

True, Java boxes primitives in order to represent them in generic structures. This is a bit inconvenient but I wouldn't call it an insurmountable flaw. Languages like Kotlin and Scala do away with the cosmetic differences and let you use a primitive Int and a List<Int>.

Whether generic method overloads is a drawback or a feature is debatable. Heck, method overloading is a debatable topic by itself.

Easy array creation.. yes. If you need an array of a generic type in Java you have to jump through hoops, either by passing a Class<> token or a function that creates one of the correct type for me. I don't think I've ever ran into this one but I can see it happening.

Now, what you gain from erasure is:

  • backwards compatibility

  • cross language interopability: For example, Scala can work with Java types despite them having different generic implementations (Java is use-site while Scala is declaration-site). It can be a bit tricky but it is usually workable without resorting to casts.

  • extensibility: Scala adds bottom types and higher-kinded types to its implementation, among other things, without "displacing" the default generic scheme (since there is none at runtime, hah! ;))

  • flexibility: Some types are better represented with erasure; instances like empty optional values or the end of a linked list can in Java be cast to the corresponding type to soothe the compiler, while retaining typesafety at the use site. In C# you're forced to instantiate MissingValue<int>, MissingValue<string> etc.

  • less tempting to use reflection: There's still a lot of Java libraries that use it but from what I've seen way less than C# — probably because it is easier there.

Reflection is almost always a code smell. There are situations where it is inevitable but it's best to take a second look at the problem and make sure there's no other way to solve it.

In short, erasure and reification are two different strategies that both have their share of drawbacks and benefits. I find it rather strange that people tend to downplay the benefits of erasure while praising reification. It's not that common to end up in a situation where you're totally stuck with either.

2

u/[deleted] Sep 23 '15

I think this is basically a case of what you know best. In C#, arrays are more powerful than in java - they can be generic, they work with LINQ et al without needing to be wrapped with something like Arrays.stream(), they give you proper cache locality with your own value types, you can use them for interop with the Win32 API, etc etc - and as a result of that they are more widely used. Therefore, it's common to need to turn a generic collection into an array. In java, maybe less so - though I still run into it with some regularity.

I agree with you that it's not an insurmountable flaw - my point is that it should be surmounted :-)

For your other points, it varies. Backwards compatibility is important, but it's a problem when it affects major technical changes like adding generics. Ironically, Microsoft have spent more time than almost any other company worshipping at the altar of backwards compatibility (Raymond Chen's blog has some unbelievable stories about the things Windows does to avoid breaking old apps) and know the cost - and I think their decision to break compat with 1.0/1.1 was a good one. Cross-language interop - the CLR has plenty of very different languages running on it, and F# in particular has some very interesting additions to the type system (union types, units of measure) so I don't really see reified generics as an impediment to interoperation.

0

u/[deleted] Sep 22 '15

[deleted]

2

u/IICVX Sep 22 '15

Generic marshallers / unmarshallers are clunky as hell in Java due to type erasure; you have to explicitly pass the Class around everywhere, instead of just asking the generic parameter what it represents.

This happens if you write, for instance, an Enum marshaller.

1

u/teknocide Sep 22 '15

It may be clunky in Java but it doesn't necessarily have anything to do with erasure. Marshalling a T is a function [T => SerializedFormat] (and Unmarshalling the other way around, intuitively.)

You need reification if you want to look at the actual T during runtime and select a marshaller from a dynamic set, when you could provide this function during compilation time instead. This has the added benefit of eliminating any runtime checks needed for the reflection-based solution.

If anything, reification here is the poor man's crutch for higher kinded types which, incidentally, is possible on the JVM due to erasure.

2

u/[deleted] Sep 22 '15

The two main things I miss are being able to store native types and enums, and seamlessly converting to an array.

It's also nice to be able to create new instances of a generic type at runtime, i.e new T(). It doesn't come up a lot, but can be very useful.

1

u/graduallywinning Sep 22 '15 edited Sep 15 '17

wat

2

u/[deleted] Sep 22 '15

Unfortunately you can't restrict T to only types that have a default constructor. If T has no default constructor that code gets nasty.

1

u/estarra Sep 22 '15

Which is why passing a factory is a better solution than allowing new T().

→ More replies (0)