Though the one-character difference between the two keywords might cause some confusion about whether a local is final or not, and it would break existing code that uses any variables named "val" (as undescriptive as it is, it's still pretty common). "var/let" would be more reasonable for these reasons, IMO.
it would break existing code that uses any variables named "val"
Would it? Are all keywords in java reserved and unusable as variable names? (var would be problematic too as well). When C# introduced var there was no compatibility issues as var wasn't valid anywhere where variable names were valid, and if a type in scope happened to be named var (which would go against the naming conventions anyways) that type would simply be used instead of the keyword. In C# the following program compiles perfectly fine before and after the introduction of var
void Main()
{
var var = new var();
var.num = 4;
}
// Define other methods and classes here
class var
{
public int num{get;set;}
}
and using int var = 5; is likewise perfectly fine. If you don't have a var type then var var = "var"; creates a variable named var that is type inferred as a string. I don't know if the Java team wants to create this context sensitive complex grammar, but if they are willing to then it can be completely backwards compatible.
Just checked with Java keyword usability as variable names.
If I do
int int = 5;
it errors with
[javac] file.java:67: error: not a statement
[javac] int int = 5;
[javac] ^
[javac] file.java:67: error: ';' expected
[javac] int int = 5;
[javac] ^
[javac] file.java:67: error: not a statement
[javac] int int = 5;
[javac] ^
[javac] file.java:67: error: ';' expected
[javac] int int = 5;
As much as I'm voting for var/val in C# I do think Java would benefit from just doing final since everyone already knows that keyword and what it means in Java.
really the immutable reference should be the shortest one to type so that it's default. too late for java, of course. this is something i think rust got right. let vs mut let.
Or say that we are talking about the (im)mutability of variables, not values.
I'm not sure talking about "shallow immutability" is appropriate in this context. When you do a shallow copy, only the fields will be copied (i.e. copies of primitives, and copies of references), so this suggests that shallow immutability means the fields will also not be re-assignable.
88
u/omair-majid Mar 09 '16
They are holding a survey on keywords (var, var/val, var/let): https://www.surveymonkey.com/r/KGPTHCG
Bikeshed away!