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;
If it were a reserved keyword then it would certainly be a problem, but the proposal suggests incorporating "var"/"val"/etc as reserved type names (I.e., not keywords) specifically for backward-compatibility. This would only break pre-implementation packages using the types "var"/"val"/etc, which (as they mention) break Java conventions for type names anyway (Pascal case).
Although frankly primitives as keywords and "var" as just a reserved type doesn't seem quite fair..
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!