Make Constants Defined in a Procedure Local

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Make Constants Defined in a Procedure Local

Post by c4s »

When a constant is defined in a procedure, the constant should only be available in that procedure. Similar as it is with labels (was implemented kind of recently).

One of the reasons is that it doesn't make much sense the way it is right now:

Code: Select all

#Global = "Global Constant"

Procedure Test()
	#Local = "Local Constant"
EndProcedure


Debug "Global: " + #Global  ; Ok
Debug "Global: " + #Local  ; Doesn't make sense!
The other reason is that I like to put certain constant values, only used for one calculation or whatever, in constants (to increase readability). Unfortunately it often doesn't work:

Code: Select all

Procedure CalcThis()
	#ITERATIONS = 10
	#MAX_VALUE = 999
	; [...]
EndProcedure

Procedure CalcThat()
	#ITERATIONS = 10000  ; Doesn't work :(
	#MAX_VALUE = 1.0
	; [...]
EndProcedure
...And please don't tell me that I should rename those constants to "fix" the issue. ;)
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
DontTalkToMe
Enthusiast
Enthusiast
Posts: 334
Joined: Mon Feb 04, 2013 5:28 pm

Re: Make Constants Defined in a Procedure Local

Post by DontTalkToMe »

The current implementation make sense because PB constants are like #define in C and similar to macros substitutions.

To properly do what you are asking it would require a type qualifier like const to be applied to an ordinary variable to make it constant.

It has been already requested but to no avail.

Optionally could mean much more than that but I think that is out of reach for us.

A very limited const like the one you are asking for could be a good start and useful in its own right.

Good luck :)
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Re: Make Constants Defined in a Procedure Local

Post by GPI »

Maybe this help: In Moduls constant, procedures and so on are "local".
Post Reply