Explicit mode
Closed this issue · 4 comments
There was a voting on Twitter about this topic and it seems that while the majority of voters are okay with dropping sigils in the upcoming version, still many of them are conservative and would keep on coding the sigil-style.
I think we should go both ways, just like QB64 does and allow both styles. Here is an early idea.
A compiler directive (PRAGMA) should declare whether the code uses the old style or whishes to define variables explicitly. The programmer may go on the old style if they set the following directive in the beginning of the program:
PRAGMA implicit_declaration = 1
LET undefd_byte! = 42
However if the directive is unset or is set to 0, the new style is used:
DIM my_var AS BYTE
LET my_var = 42
LET undefd_var = 42 : REM !!! this will throw a compiler error
In this case you can still use the $#%!
characters at the end of the variable names but they will be ignored by the compiler.
Other things to take into consideration:
- Function definitions should also use the
AS
keyword, e. gFUN my_fun (param AS type) AS type
- Built-in functions must be able to auto select the correct type, e.g
LET x! = RND()
should work by automatically choosing the byte type of RND. This may turn out to be very challenging to implement. For examplePRINT my_fun(RND())
- which type of RND to call? - The
CAST
keyword must be changed, e. g.CAST expression AS type
. Optionally we may introduce new functions likeASBYTE()
,ASINT()
etc DATA
statements must also useAS
keyword
Please add your thoughts.
I wasn't thinking about the built-in functions, the casting and the DATA
statement when I proposed on Twitter to go the way of QuickBASIC. Taking all that into account, I believe that's too much trouble to do all that adapting work.
I think the most important thing is the explicit definition of variables by default: that can save a lot of trouble with debugging, especially if you don't/can't use an auto-completing editor while writing the XC=Basic code. It's too easy to misspell a variable name and cause some obscure trouble that can be hard to debug. After all, that's why the explicit definition was introduced in other languages 🙂
Since this is a BASIC dialect for old platforms, I think you could keep the sigils: I grew up with TI 99 4/A Basic, then ZX Spectrum Basic, and then C64 Basic. All of those used "$" for string variables, and the C64 dialect used other sigils too, so I don't see a problem with keeping using those. I'm fine with "#" identifying "semi-long" variables, for instance.
When I started using XC=Basic, I got immediately comfortable with the sigils you implemented, and I relied on the compiler to error out whenever I forgot to add the "!" sigil to my byte variables.
So, to recap, my suggestion is: do implement PRAGMA
to be backwards-compatible with old programs, make explicit definition of variables the default, and keep the rest as it is 🙂
Yes, explicit declaration will save from errors caused by misspelled variable names. That's one thing. The other thing is that it's very easy to forget to append the sigil to the variable name causing a compiler error. If there is a solution for function calls, sigils won't be needed any more (in explicit mode).
I'd far prefer type definitions, as I often forget to add the sigils after I've defined a variable, and have to go back and correct my code.
This is probably just me being new to the language though, and I'm sure I'd get used to sigils over time, if implementing type definitions causes too many problems.
Entirely rewritten in V3.