Poll: Unseen is crazy...
You do not have permission to vote in this poll.
Defo...no doubt...
100.00%
1 100.00%
Defo again - but he may actually be on to something here...
0%
0 0%
Total 1 vote(s) 100%
* You voted for this item. [Show Results]

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Arbitrary CONST values
#11
(10-26-2025, 06:06 PM)CharlieJV Wrote:
Quote:...snip...

CONSTANT's in someones code must be known at COMPILE time.     Any parameter that is system dependent or can change on the same system between program runs can NOT BE A CONSTANT ! .
Just a thought:

If I have a program that needs to remember the date and time when the program started, that is a CONSTANT for the specific run of that program.

The "run start date and time" value is different for every run, but it is constant within one run, and is never known at compile time.

  I understand what your saying there.   But a CONSTANT in the context of a Computer language is a very specific concept.


When you do
   CONST PI = 3.1416

  The compiler will usually insert that immediate value in the code at every place PI is referenced.    If on the other hand you do

DIM PI AS DOUBLE
PI = 3.1416

   Then your code will pretty much work the same as long as you DON'T CHANGE PI.   But the code generated will be referencing a variable in memory and not an immediate value.      There would be nothing to keep you from putting in your code.    PI = 3.0   REM   So There !!!!
Reply
#12
@ahenry3068

I think Charlie is saying 
Const MyDate$ = Date$, MyTime$ = Time$

These will remain constant through Run of the program, even though every time you start a new run they will be different. So there is that too!
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#13
(10-26-2025, 10:50 PM)bplus Wrote: @ahenry3068

I think Charlie is saying 
Const MyDate$ = Date$, MyTime$ = Time$

These will remain constant through Run of the program, even though every time you start a new run they will be different. So there is that too!

     Yes.   I get what he said.   That is exactly as I understood it.    Those things are still not known at compile time though.  
Being known at compile time and NOT CHANGING ever is the whole point of a CONSTANT in any programming language.

  Allowing the code you put above would be contrary to the whole purpose of having a CONSTANT directive.      

I've been doing a whole lot of assembly code lately, maybe that's why I feel so strongly about this.   

We are talking about what machine code is generated.   


When you use a CONSTANT value the code generated is like this

CONST FOUR=4
X = FOUR       
D = FOUR

Would generate the EXACT same code as

X = 4
D = 4

ML CODE WOULD BE LIKE

LOAD REGISTER IMMEDIATE VALUE 4
STORE REGISTER AT ADDRESS X

LOAD REGISTER IMMEDIATE VALUE 4 ; this could possibly even be eliminated with a good code optimizer, but would probably be there without
optimization. Immediate register loads are usually much faster than Memory loads too.
STORE REGISTER AT ADDRESS D




THIS: 

DIM X AS INTEGER
X = 4
D = X 

Would generate quite different and more complex code at the machine level. 

LD REGISTER WITH IMMEDIATE VALUE 4
STORE REGISTER AT ADDRESS X

LD SRC POINTER REGISTER WITH ADDRESS OF X
LD DEST POINTER REGISTER WITH ADDRESS OF D

MOV VALUE AT SRC POINTER TO DEST POINTER LOCATION


This is a pretty simple example. But the whole point of CONSTANT declarations is to speed up and simplify the generated code !



Charlie is missing the point of constants.
What he wants would make it NO LONGER A CONSTANT !
Reply
#14
Well hell! I'm not going to argue with an assembly programmer. 

I do know I would never recommend QB64 to a Computer Scientist because they would learn so many bad habits! and be so mocked by their cohorts for programming with line numbers. 

WTH? calling a varaible set by a Function at Run time a Const what were they thinking??? What sacrilege!
UPDATE: Well turns out the IDE wont let me do this, I was mistaken.

And we wont even get into GoTo Smile

@ahenry3068 what are you doing hanging out here? are you slumming?  LOL
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#15
Guys, I think some of you need a lesson in CONST vs unchanging VARIABLE.

A constant is a name given to a literal value.  This value is one thing, one thing only, and will forever be that that one thing.  You determine what that thing is BEFORE compiling the code, use the name in your code, and that LITERAL VALUE is placed into the code and compiled.  We use CONST as programmers as it's easier than remembering set values.

Let me toss some values:

Byte_Limit = 2 ^ 7 - 1
Integer_Limit = 2 ^ 15 - 1
Long_Limit = 2 ^ 31 - 1
Integer64_Limit = 2 ^ 63 -1

Now, I can remember that byte's limit is 127 (or 2 ^ 7 -1), so I can swap those easily back and forth in my programs, but somebody tell me what the integer64 limit is.   Go on.  Off the top of your head, recite that value with 100% certainty.  It *NEVER* changes.  It's *ALWAYS* the same value.   But WHAT is that value?   It's a mighty big damn number.  Do you have every digit memorized perfectly?  Did you type every digit perfectly?

So instead of using the value directly, or even worse, using the unchanging formula above, we just use the CONST value as we have defined them in our code.   The QB64PE compiler then sees that CONST, and as it translates your BASIC to C code, it substitutes that LITERAL calculated value directly for the CONST.

Code like:   PRINT Byte_Limit
Becomes compiled as:   PRINT 127

The compiler sees the LITERAL value; we see the CONST value for ease on our own understanding and usage.



Now, a program that starts up and runs once each day of the week might have code that determines:

TODAY = DayOfWeek

Now, all day long, like Charlie's example, this doesn't change.  The program is set on a timed task.  It runs from 8AM when your business starts up, shuts down at 10PM when your business closes at night.   It's an UNCHANGING VARIABLE.

This is *NOT* a constant.   It's not some literal in the code.   You can't just say:

TODAY = "MONDAY"   and then have it right all seven days of the week.  You're getting the date, determining the day of the week, and storing it at runtime.  It's not ALWAYS the same.  You want it to change every day; you can't substitute a LITERAL unchanging value in its place and keep it valid, ergo it CAN NOT be a CONST.   



And the same with Unseen's original idea.

CONST DW = _DesktopWidth

_DesktopWidth is a function that returns a value when its called.  That function can be variable in its results.  If you want it to be CONST, then what you want is:

CONST DW = 1920

See?  A LITERAL, neverchanging value.   

With CONST DW = _DESKTOPWIDTH, you could conceivably get the desktop resolution at compile time, make an EXE, and then have the literal value of your screen width stored in that compiled EXE forever.... Of course, if you buy a bigger monitor, it won't change or scale.  If you move the EXE to a different machine, it won't change or scale.  If you change resolution on your monitor, it won't change or scale. 

You have.... defeated the whole purpose of that command being a function.  It doesn't set that value at RUN TIME.  It sets it at COMPILE TIME.   If that's what you want, why don't you just right click on your desktop, hit DISPLAY SETTINGS when the popup appears, see what your values are, and then set them yourself?

CONST DW = 1920, DH = 1200

Presto!  Permanent CONST values which will always be set in stone to match your screen when you wrote it.

To consider the idea in another perspective, it would be the exact same as:

CONST Today$ = DATE$

So your Today is going to be forever locked as the date you compiled, never changing, never really being Today... It'll just be "10/26/2025" forever.  (As that's the day I wrote this post.  Tomorrow, it won't change to update to "10/27/2025" and when folks read this a month from now, it won't match their day either.)




A variable you set one time and never change is NOT a constant.  

A constant is a verbal descriptor of a LITERAL value.

One = 1
Two = 2
Three = 3

If your program is going to be:

SELECT CASE DayOfWeek
   CASE "Monday": One = 1
   CASE "Tuesday": One = 123
   CASE "Weddingsday": One = 3.14157
END SELECT

Then that's NOT a CONST.

PI isn't 3.14159  on some days and 9.8763 on others.  It's a literal, never changing value.  We type PI because it's easier than typing 3.141592654....   

See the difference in the two?

A CONST is a literal value, substituted in your code at compile time and only used for the programmer's benefit.
An unchanging variable is something you set once and then simply never alter the value of again inside your program.

It really is that simple of a distinction, but that's a pretty damn important distinction to make.
Reply
#16
Quote:Charlie is missing the point of constants.
Um, no, Charlie is not missing the point.

Charlie was just sharing an insta-thought-out-of-nowhere about constants that I found interesting.  It wasn't meant to be a "this is what constants are" dogmatic opinion.

Silly me: I was under the impression that a  "Just a thought" opening was very different from an opening like "My dogmatic opinion"...

i.e., for me, "Just a thought" is the short form of "Just a thought to philosophically ponder for the $hit$ and the giggle$."


So much for me thinking this would be a fun distraction from the discomfort of shingles...
Reply
#17
Quote:Those things are still not known at compile time though.
See, I knew I was crazy! This one comment for me at least, explains it all!

Thanks

John
Reply
#18
(10-27-2025, 01:48 AM)bplus Wrote: Well hell! I'm not going to argue with an assembly programmer. 

I do know I would never recommend QB64 to a Computer Scientist because they would learn so many bad habits! and be so mocked by their cohorts for programming with line numbers. 

WTH? calling a varaible set by a Function at Run time a Const what were they thinking??? What sacrilege!

And we wont even get into GoTo Smile

@ahenry3068 what are you doing hanging out here? are you slumming?  LOL
    I like doing a lot of different stuff.    And I'm not a wizard.   The assembly I have been doing is 8 bit these days (I have done x86 in the distant past).   

And Charlie.    I wasn't putting you down or trying to get into a huge existential argument.    But to me compiler terminology has to mean something.  And CONSTANT is a concept that transfers across a lot of different compilers.      I've been dealing with some old development methods where they aren't available to me (Commodore BASIC Smile )   and I use variables that I don't change for the same purpose in my code.    But I make sure that when I'm discussing my code on those boards that I use the term **Psuedo Constant** for what I'm doing.    And it helps me keep it straight in my own head.

BTW Assembly code is actually why I don't have a problem with BASIC GOTO . Assembly doesn't have anything like SELECT CASE or IF THEN ELSE . It has a CALL and conditional and unconditional branch instructions. (I'm generalizing a lot, different CPU's have different instruction sets) To program in assembly you certainly must be prepared to deal with GOTO
Reply
#19
I stand corrected! +1 Thankyou @SMcNeill and @ahenry3068, I am glad to know more about you.

Note: the IDE wont even let me write: 
Const MyDate$ = Date$

+1 to @Unseen Machine too, I learned from this topic in fun way, crazy or crazy like a fox?

@CharlieJV we'll get 'em next time Smile
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#20
While I generally agree that a constant should be know at compile-time, some languages do offer const-qualified objects where a variable becomes read-only after its initial assignment. I think that was the original ask - which is completely valid IMO. QB64 does not have anything like that ATM. But I think it may be a useful feature.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Upcoming changes to CONST may affect your code SMcNeill 14 2,744 12-30-2023, 05:13 PM
Last Post: Kernelpanic
  TYPE and CONST within SUB/FUNCTION TerryRitchie 9 1,952 07-11-2023, 01:22 AM
Last Post: bplus
  suggestion: initialize array values within the DIM statement bobalooie 19 3,356 12-22-2022, 06:46 PM
Last Post: Kernelpanic
  Convert IPv4 dotted quad into four decimal values bert22306 6 1,548 10-31-2022, 03:30 AM
Last Post: bert22306
Bug Color CONST Warning v1.5+ bplus 3 809 05-05-2022, 06:13 PM
Last Post: Pete

Forum Jump:


Users browsing this thread: 1 Guest(s)