If "..." ISN'T in your control, then...don't use "var".
If you work with other developers that don't understand the above 2, review their code, influence them, convince them. If you work with so many of such people that you cannot convince them of the value of this, go work somewhere else.
You have lost a huge debugging technique, using your eyes, for your lack of patients for writing a variable type.
Something doesn't just stop being a problem because an IDE helps you avoid it.
The explicit nature of Java is what makes it great. What you type is what happens.
And using var doesn't suddenly make things happen randomly, everything is still perfectly well defined, just less verbose. And it is a feature that 99.9 % of the time does what you want, situations in which you want a different type than the inferred one are rare in practice. Actually the only one that I can remember is initializing a variable with some concrete implementation of an interface and desiring the interface type instead of the concrete type. And this one occurs pretty rarely because I think aiming for single assignment is a good thing.
While typing a variable name explicitly is a feature that 100% of the time does what you want.
HashMap<String, AtomicReference<Foo>> fooMap = new HashMap<String, AtomicReference<Foo>>();
What is the disadvantage of using var? var fooMap = new HashMap<String, AtomicReference<Foo>>();
It's less redundant, easier to read, does not clutter the code and has exactly the same information content. And if you really want the variable to be of type Map<,> nobody stops you.It's been in C# for a few versions, and you do find there are places where it always makes sense to use (foreach loops), and there are places where you want to use some judgement. Of course all variables still have a type, it's just not made explicit in the source code, so you can get your IDE to tell you what it is (e.g. by hovering).
If this is widely adopted, I'd say that there will be a lot of problems that stem from it. That's why I say this shouldn't be a feature.
Hell, I'd have a hard time debugging new code if I saw var x, y, z;
Var means nothing! It has no implicit meaning. It just means whoever wrote the code was too lazy to write out the type names.
If you agree with that or not, just know that the few seconds it would have taken you to type out a full type name will probably save someone debugging your code in the future hours and make it infinity easier for someone who comes along and needs to start working on your code base.
dynamic, on the other hand....<looks shiftily into C#'s direction>
J/K. I Love dynamics.
Why not?
let (a, b, c);
is perfectly valid Rust, it just declares 3 locals a, b and c. fn main() {
let (a, b, c);
a = true;
b = [1];
c = "foobar";
}
https://play.rust-lang.org/?gist=ebedd0cd5b6707410aff&versio...
Could someone please tell me the following: How do I tell the type of x?
We have to go look in callSomeRandomFunction, don't we? Now how easy will it be to do that if you have 15 variables and calls to different functions?How do you know what types are iterable and what types aren't?
What I love about the way explicit definition works is that you are almost guaranteed that in a function you have the type that you are looking for.
If they kill that, they kill one of the greatest parts of Java: the parts that make it not only easy to write but easy to understand.