Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DIM - AS - VARIABLE TYPE likely bug
#16
(05-03-2024, 06:59 AM)bartok Wrote: I have found a probable bug using AS.

Writing, for example:

DIM A%

it's the same then writing

DIM A AS INTEGER

But it is possible to write:

DIM A%
DIM A$

but we incur into a "Name already in use" error if we write:

DIM A AS INTEGER
DIM A AS STRING

Example:
Code: (Select All)

OPTION _EXPLICIT
DIM A$
DIM A% '--------------------------> working

'DIM A AS STRING
'DIM A AS INTEGER'  ---------------------> not working

A$ = "hello"
A% = 2

PRINT A$, A%

I'm not sure if this is useful, but one gets no points by not trying ...

In early BASIC implementations, we didn't declare variables.  Variables would be created upon assignment of a value to them.  (LET A$ = "howdy")

When assigning a string literal to a variable, that variable had to have a $ suffix, indicating the type of the variable before the literal value got assigned to it.

Referencing the GW-BASIC Constants and Variables chapter: a % suffix for integers, a ! suffix for single-precision floating point numbers, and # for double-precision floating point numbers.  

If the variable has no data type suffix, then it is treated as a single-precision floating point number.

The data type suffixes are part of the variable name.  If you have "LET A = 5.1" and "LET A! = 5.1", these are two independent variables, because their names are not the same.  One name is "A", the other is "A!".

Back in the day, DIM was only used for declaring the dimensions of an array, and that was only required for arrays with subscript values greater than 10.

Array names, just like variables, included a data type suffix (I prefer to say data type sigil) to define the data type.  No data type suffix, the array is then treated to hold single-precision floating point numbers.

At some point (QBASIC?), BASIC implementations started using DIM to also declare variables, with the option of using the "AS data_type" clause (allowing the riddance of those "pesky" data type suffixes).  I think this was an enhancement added part and parcel with features for structured programming (line numbers no longer required, multi-line functions, "SUB" subroutines, etc.)

However, for compatibility with previous BASIC implementations, the data type suffixes still work.

In the structured BASIC implementations, a variable referenced as A% is a variable distinct from variable "A" declared with "DIM A AS INTEGER", because the variable name "A%" is not the same as the variable name "A".

Myself being a little bit old school, I still prefer variable (and constant/array) names with data type suffixes.  Although variables don't have to be declared when using suffixes, I like the idea of declaring all variables before the program's code.  So DIM A$.

For me, there's no philosophical debate about it.  It is a design-decision based on what works for my brain, everybody else can take my code or leave it.

If a project is really interesting to me (and/or I'm getting paid to contribute), I'll adopt whatever standard is best for that project and the people leading it (so I'm quite happy to toss my preferences aside.)
Reply


Messages In This Thread
DIM - AS - VARIABLE TYPE likely bug - by bartok - 05-03-2024, 06:59 AM
RE: DIM - AS - VARIABLE TYPE likely bug - by CharlieJV - 05-05-2024, 03:44 AM



Users browsing this thread: 3 Guest(s)