1) Diamond operator for generics 2) Type inference for lambdas
Naturally would be good to get even more of it.
int result = someFunc()
you see val result = someFunc()
Is anything really gained other than saving a couple obvious (and compiler checked) keystrokes? And you've lost the ability to see the type of each variable locally in the function. It's never seemed very useful to me.int result = doSomething();
but
var result = new Dictionary<string, int>();
Type inference really shines once you get into evolved lambdas and generics.
We had a developer here that used 'val' from lombok all over the place and it was irritating. The team made a collective decision to go through and eliminate this from the code base after he left.
I don't want any of this in our code.
But it's not uncommon (in my opinion) to write stuff like
EnumMap<FooEnum, BarClass> map = new EnumMap<FooEnum, BarClass>(FooEnum.class);
which to me ought to read something like var map = new EnumMap<FooEnum, BarClass>();
Of course, I don't do a lot of Java programming these days and perhaps my first example is simply broken due to ignorance.When I wrote something very similiar to that last week, I failed to figure out how to avoid repeating the type signature, and was annoyed.
EnumMap<FooEnum, BarClass> map = new EnumMap<>(FooEnum.class);
using the "diamond operator" [1]. In fact, my IDE even provides automatic hints to make use of it!With that said, I do think java has too much pro-long-method-and-variable-name culture.
[1] http://www.javaworld.com/article/2074080/core-java/jdk-7--th...
Map<SomeComplexTypes> map = new HashMap<>();
ArrayBlockingQueue<Work> q = new ArrayBlockingQueue<Work>(10) BlockingQueue<Work> q = new ArrayBlockingQueue<>(10)
right? You'd use the interface type that you'd want to be working on, so you could replace the ArrayBlockingQueue if needed and be sure you're not using any specific functions.Still seems better than having a 'val' in there, since it signals exactly what aspect of the object you're using. Plus now the compiler will complain if you change ArrayBlockingQueue to something that isn't a queue.
I just don't really see the benefit - so what if it saves a couple keystrokes? I don't think I've ever run out of keystrokes before... Meanwhile if it saves a single logic error or helps the compiler detect a mistake, then it's saved a lot of real effort.
I know this is just an example, but if you actually came up with something more realistic, you would have seen that your issue is actually a non-issue.