This isn't about changing Java to a weakly typed language, though. Java currently determines types via RHS (with a bit of LHS scanning for the diamond operator). If you instantiate a variable with a concrete type on the RHS, why do you need to declare its type on the LHS as well? You've already stated what you want when you instantiated it.
This is simply syntactic sugar to switch this Map<String, List<Class<?>>> foo = new HashMap<>(); for this var foo = new HashMap<String, List<Class<?>>>();.
Generally C# encourages you to use var when its clear what the type is.
var x = new MySpecialObject();
var y = "some string";
var z = Factory.GenerateMyObject();
Additionally, because its implicit typing your intellisense will still work even when it's not clear what the object type is.
Part of C# take on it is that you cannot RETURN implicitly typed object.
HOWEVER
C# supports dynamic typing. Its generally used in very special circumstances and it makes you incredibly aware that something could go wrong so dont use it unless you REALLY, REALLY have to. You can even return dynamic as a type.
How this differs from var is that it isnt implicit, its type is UNKNOWN at compile time and only once the code is run can it check if .GetValue() is a method of that type.
It's worthwhile to note that whenever dynamic is mentioned on /r/csharp, a comment on unmaintainability inevitably comes up.
I think the value of var is that it makes you think more abstractly about your code - you aren't dealing with integers anymore, you're dealing with numerics, or strings - it just reads better.
I also think the argument against 'var' yielding code with hard-to-reason-about code falls through once you see other languages (e.g. JavaScript) successfully having var/let. IMO, It ultimately boils down to yes, if you have terrible variable names (x, y, z) your'e going to have terribly unmaintainable code. But if you name by convention (usersById is clearly Map<int, User>) or clearly (var firstName is certainly a string) then it won't be an issue, and you're forced to think more abstractly "Hey, I'm working with a name" which just so happens to also be a string.
How much duplication is in this code?:
Map<int, User> usersByName = new HashMap<int, User>();
I'd argue 3. C# lets you get down to 2 with:
var usersByName = new HashMap<int, User>();
C++ would let us say this:
HashMap<int, User> usersByName;
But JavaScript (and likely more dynamic languages like python/ruby) let us do this:
Not fair to compare javascript's total lack of typing with C++/C#/java. Compare to python or ruby if you wish. but of COURSE dynamically typed languages have less typing.
73
u/Northeastpaw Mar 09 '16
This isn't about changing Java to a weakly typed language, though. Java currently determines types via RHS (with a bit of LHS scanning for the diamond operator). If you instantiate a variable with a concrete type on the RHS, why do you need to declare its type on the LHS as well? You've already stated what you want when you instantiated it.
This is simply syntactic sugar to switch this
Map<String, List<Class<?>>> foo = new HashMap<>();for thisvar foo = new HashMap<String, List<Class<?>>>();.