开发者

Why is it illegal for variables to start with numbers?

Why is it illegal for variables to start with numbers?I kno开发者_如何学Cw it's a convention but what's the reason?

Edit: I mean variables like "1foo" or "23bar" not only numbers like "3"


Because the lexer in most languages will assume you are trying to specify a numeric literal. And then you could declare variables that are indistinguishable from numeric literals, creating a huge bombshell of ambiguity.


Pop quiz: in a hypothetical language that permits a variable to begin with a number, what is this?

0xDEADBEEF

In C (and related languages) this can only be a hexadecimal number. If a language allows a variable name to begin with a digit, that could be a variable or a hexadecimal number. That's one quick example of potentially millions.


Numbers are interpreted 'as is' without any syntax whereas strings/characters are mostly represented with quotes.

So, the program can understand the difference between a variable name containing characters and a string of characters but it does not goes the same with numerals.


One reason, probably the most obvious one, is that it would make your life more difficult, without bringing anything reasonably useful to the table. For example, in C, you wouldn't be able to tell whether a string of digits is an identifier or a numeric literal.

int 10 = 15;
int 15 = 10 + 5;

In the second line, is 10 a variable holding the numeric literal 15 or is it the numeric literal 10?

Another reason is that allowing a variable name to begin with a digit makes error checking during compilation a lot more complicated, again, without bringing anything reasonably useful to the table.


In languages such as Prolog, Erlang, and some early versions of Fortran, you very nearly got to do this, for completely different reasons.

Prolog/Erlang don't have variable assignment, they have unification. IIRC, if X is a variable, then code following 2 = X, or X = 2 is processed if X may have the value 2. So if X is already unified with a value, then that value must be 2, and if not, X becomes 2 from then on. So writing 3 = 3 is fine - it should become a no-op, and 2 = 3 always fails - either a non-match in Prolog or (I think) a runtime error in Erlang. Numbers behave like variables which have already been unified with the value the numbers represent.

In early Fortran ( apologies for not having used fortran in twenty years and forgetting its syntax ), all function arguments were passed by reference, so if you have a function which was equivalent to void foo ( int &x ) { x = 3; } and called it with a number, the compiler would store the number in a static variable and pass that. So calling foo (2) would set that static stored value of 2 to 3. If it happened to use the same static variable for the literal 2 somewhere else, such as calling another function with the literal 2, then the value passed to the second function would be 3 instead.

So you can have variables which are syntactically identical to numbers, as long as they are automatically initialised to the value of the literal. But if you allow them to be mutable rather pure variables, weirdness abounds.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜