11-19-2023, 06:18 PM
Is there a reason why a variable name can not begin with a numerical value? ie a1 = 100 is ok but 1a = 100 or 1a$ = "100" is not ok.
Variable Names
|
11-19-2023, 06:18 PM
Is there a reason why a variable name can not begin with a numerical value? ie a1 = 100 is ok but 1a = 100 or 1a$ = "100" is not ok.
11-19-2023, 06:46 PM
(11-19-2023, 06:18 PM)Dimster Wrote: Is there a reason why a variable name can not begin with a numerical value? ie a1 = 100 is ok but 1a = 100 or 1a$ = "100" is not ok. https://qb64phoenix.com/qb64wiki/index.php/Variable Guess it's for historic reasons aka QB4.5 compatiblity.
GuiTools, Blankers & other Projects:
https://qb64phoenix.com/forum/forumdisplay.php?fid=32 Libraries & useful Functions: https://qb64phoenix.com/forum/forumdisplay.php?fid=23
11-19-2023, 07:46 PM
(11-19-2023, 06:18 PM)Dimster Wrote: Is there a reason why a variable name can not begin with a numerical value? ie a1 = 100 is ok but 1a = 100 or 1a$ = "100" is not ok. Not that I know a heck of a whole lot about interpreters/compilers parsing and tokenising (?) a program, but I imagine there's value in the process knowing immediately that:
I think.
11-19-2023, 07:52 PM
It would make parsers explode.
11-19-2023, 07:58 PM
It's a text parsing problem.
Write your own compiler and see how difficult it becomes when what appears to be numbers at first are no longer numbers because it has some other non-number in it. Especially problematic for a language like BASIC where a typo can bring another variable into existence to cause a difficult to track down bug.
11-19-2023, 08:12 PM
If I had to attribute the choice to anything, it'd probably be simply to the way that VAL behaves.
VAL("123abc") = 123 VAL("abc123") = 0 VAL takes all the numbers from the start and then assembles them into a valid numeric value, stopping whenever an unexpected character is found. If I have to venture a guess, I'd say the original creators of BASIC used some logic, based around this concept, to parse what makes a number and what could represent a keyword or variable. Since we tend to try and follow those some historical rules as what was set by QB45 and the folks previously, we don't tend to allow numbers before letters in variables either. Which reminds me of this old article of interesting fact: Quote:The Space Shuttle and the Horse's Rear End
11-19-2023, 08:23 PM
Original Basic used single letters for variables and tended to that to save precious Ram space.
That's my theory of where it all started, no cavemen involved sadly.
b = b + ...
11-19-2023, 09:01 PM
To be able to use a variable name like "1a" we would all have to use "LET" again. Obligatorily.
It's simply because the earliest versions of BASIC supported line numbers! Yes remember that? Couldn't have a program in GW-BASIC for example without line numbers. And in an 8-bit computer like my Radio Shack TRS-80 Color Computer 2, "1a=3" would be broken down to: Code: (Select All) 1 a = 3 The earliest BASIC's on 8-bit computers allowed almost all spaces omitted when composing programs because a space actually took up room in memory, and back then those computers had to work with 16KB or less RAM. Oh well C and Lua and many other programming languages also don't permit numerals to begin a variable name, for other reasons. Dimster why do you want to have a variable like "1a" anyway?
you could use Lisp
(setq 1a 123) 123 (expt 1a 20) 628206215175202159781085149496179361969201
11-19-2023, 11:01 PM
To pile on to all the stuff others have already pointed out ( ), there are numeric literals that include alphanumeric characters, so allowing variables to start with numerals create pretty bad ambiguities even in QB45/QB64 where spaces are required around most things:
Code: (Select All)
Of course, this could be fixed by simply not allowing you to use variable names that could be confused with a numeric literal, but: 1. That's a lot of extra work for little gain, simply banning variables from starting with a numeric is much simpler. 2. It would prevent us from adding any new numeric syntax's in the future if they would conflict with allowed variable names. There's probably some other conflicts that I haven't considered, but that's the most obvious to me. |
« Next Oldest | Next Newest »
|