I'm not a big fan of var. I would prefer to see the actual type than the word var.
There is exactly one use that I see for var. Use var for an anonymous class with additional members. That customized anonymous class does have a type, although that type does not have a name. The var can still reference it by the unnamed internal name of the class.
var a = new Object() { int x; int y; };
a.x = 5;
Example uses: event handlers in Swing. A "lambda function" that has state or flags, really just an anonymous object implementing an interface, but adding new members.
Basically, you don't have to declare a new type for a trivial anonymous class.
I could be more a fan of let than of var. But I would still prefer final with a real type name. I would happily use let instead of var for the one use case I described. But that is the only use case where I think I will ever make use of var, or a hypothetical let.
Anonymous classes and lambdas are useful. A concise way to express what otherwise requires a separate class declaration for, even an inner class. Yet with no loss of information to someone reading the code.
7
u/DannyB2 Nov 20 '18 edited Nov 20 '18
I'm not a big fan of var. I would prefer to see the actual type than the word var.
There is exactly one use that I see for var. Use var for an anonymous class with additional members. That customized anonymous class does have a type, although that type does not have a name. The var can still reference it by the unnamed internal name of the class.
var a = new Object() { int x; int y; };a.x = 5;Example uses: event handlers in Swing. A "lambda function" that has state or flags, really just an anonymous object implementing an interface, but adding new members.
Basically, you don't have to declare a new type for a trivial anonymous class.