There is the rule because it is ambiguous. If the variable starts with a number the compiler does not know it is a symbol (a variable, for example). He’ll think it’s a numerical literal.
Note that even here on the site when you use the number starting the name, the colorization thinks it is a literal (it is true that in these specific cases you can know that is not a literal, but then would have trouble identifying).
This is especially complicated when literals can have letters at the end to differentiate the type. If this did not exist in the language, it would even be possible for the compiler to get away with it and find out what it really is. It is true that it still gives this by existing but only in certain cases. It is better not to try this. The gain would be tiny and would treat other confusions.
Names only with numbers is not possible under any circumstances, would not be able to differentiate. Okay, I could do a Gambi, when you’re declaring (if the syntax makes it clear that it is a statement it could accept as being an identifier rather than a number, then every time it finds it in the code it will consider it to be the identifier and it cannot in that code have that literal number being used. But why all the crazy?
PHP may be more permissive with some symbols since the language requires a specific character to indicate that it is a variable.
Some languages could do this with an optional character of escape in this case, how is it possible to use the @
in C#, for example. But again, the gain does not make up for the effort, you can live without variables that start with numbers. Note however that C# allows the use of @
just to escape keywords but not names started with numbers.
If you need a name like this because it comes from a database that accepts this type of name, it is possible in C# to use an attribute to name better, not the variable, but at least to give a relation information to the column in BD.
var 2d = 1;
WriteLine(2d); //está imprimindo "1" (o valor de 2d), ou imprimindo "2.0" que é um double)
var 3 = 1;
WriteLine(3); //está imprimindo "1" (o valor de 3), ou imprimindo "3" que é um int)
There’s no way to know, it’s ambiguous.
In his example 4teste
in thesis may not be ambiguous, but why make this exception? The gain does not pay. And if you had this, could limit the expansion of language.
Let’s say that one day the language needs to make a literal that is determined by the suffix teste
. Ready, 4teste
would already be ambiguous. Of course you might find that teste
It’s a lousy suffix, but a compiler can’t judge. And language could create a mechanism that the programmer creates his own suffixes and he could be teste
or something else. If there was a variable with a number and the same name, it would complicate it. Example:
var 5dias = 1;
//um dia uma pessoa cria o sufixo dias, aí seria permitido:
var x = 5dias; //antes saberia que está guardando 1 em x, agora pode estar guardando 5 dias
I put in the Github for future reference.
Great explanation. It’s very confusing, the interpreter treats the letters after the numeric character as what exactly? Or, in any case, when there is an initial numeric character, it would treat as a type "Number", "int", "Double", etc., that’s it?
– bio
Depending on the language, C# would only treat numbers as
int
, if you have a suffixd
would bedouble
, if the suffix isL
would belong
,m
would bedecimal
, and so it goes. If it is a letter or word not accepted by the language, it would be wrong. JS would treat asNumber
.– Maniero
+1 for the double example.
– rray