More comfortable Constant handling...

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
HeX0R
Addict
Addict
Posts: 1197
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

More comfortable Constant handling...

Post by HeX0R »

I'm wondering, why the following code ends with an error message:

Code: Select all

l$ = ProgramParameter()
If l$ = "-no"
  #Debugging = #False
Else
  #Debugging = #True
EndIf
I mean something like :

Code: Select all

#Debugging = 0
#Debugging = 1
shouldn't produce errors either...
Always the first declared value will be taken and any following declaration will be ignored.
At least i know this handling from other languages.
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Post by freedimension »

look the word "constant" up in a dictionary ;-) ;-) ;-)

What you need is a variable


Oh well, I looked it up for you :D
1. [a] persistent in occurrence and unvarying in nature.

2. [a] continually recurring or continuing without interruption.

3. [n] a quantity that does not vary.

4. [a] steadfast in purpose or devotion or affection.

5. [n] a number representing a quantity assumed to have a fixed value in a specified mathematical context.

6. [a] uninterrupted in time and indefinitely long continuing.
User avatar
HeX0R
Addict
Addict
Posts: 1197
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Post by HeX0R »

I am talking about the Error-Message and the resulting impossibility to compile!

Nothing of your pasted Text says something like it is forbidden
(Well i guess thats because it isn't forbidden at all!)!
And there is of course a deeper sense for my question (not this micky-maus-code i've pasted)

btw.: None of my examples touch any of your rules, so what did you try to say me ?
GreenGiant
Enthusiast
Enthusiast
Posts: 252
Joined: Fri Feb 20, 2004 5:43 pm

Post by GreenGiant »

I think what he was trying to say is that a constant has to be, well, constant. It has to be declared at runtine, not varied during the program which requires a variable.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: More comfortable Constant handling...

Post by PB »

> I am talking about the Error-Message and the resulting impossibility to compile!

You can only declare a constant ONCE in your code. That's why you're
getting errors. You can't change it later, otherwise it's not a constant.
It doesn't matter that the declaration is inside an If/Then loop, because
the app simply can't choose one until run, and can't run until compiled.

> Always the first declared value will be taken and any following declaration will be ignored.

How can it be ignored? It's impossible. Think about it, and you'll see. Look:

Code: Select all

l$ = ProgramParameter()
If l$ = "-no"
  #Debugging = #False
Else
  #Debugging = #True
EndIf
The PureBasic compiler sees #Debugging twice and doesn't know which
value to assign to it: #False or #True? How can it know, unless the app
is run? And how can it run unless it's been compiled? See the problem?
Last edited by PB on Fri Sep 24, 2004 12:41 pm, edited 1 time in total.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Hi HeX0R,

Constants are identified with a leading # symbol, and are fixed or constant values.

They cannot be assigned a variable value.

Changing "#Debugging" to "DebuggingVar" or similar would fix the problem.


Edit:

Darn, PB, when did you sneak that post in? :) Now this response is overkill!
@}--`--,-- A rose by any other name ..
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post by GedB »

Hexor,

If you look at the ASM for some code you'll notice that the ASM makes no reference to any Constant. Not one.

This is because all constants are resolved at compile time. If you use the constant #Five as 5 then in the compiled code every instance of #Five will have been replaced with 5.

Take a look at you're code:

Code: Select all

l$ = ProgramParameter()
If l$ = "-no"
  #Debugging = #False
Else
  #Debugging = #True
EndIf 
At compile time how can the compiler possibly know the value of l$? It cannot, this variable is resolved at Runtime.

Lets examine some possible solutions.

Firstly, your own suggestion:
Always the first declared value will be taken and any following declaration will be ignored.
In this instance #Debugging will always be false because that is the first occurance of #Debugging in the text. Regardless of the command paramaters the #Debugging will always be #False.

This is clearly not what you wanted. The difference would be that now the fault is silent. You will have no warning that you're debugging mode does not work until you have deployed your executable and you really need it to fix a problem while you're customer is breathing down you're neck.

Instead we could have Constants evaluated at runtime. This will slow down you're code because now instead of 'mov eax, 5' you are using 'mov eax, [#lbl_five].

If you want to have conditional constants then use compiler directives:
http://www.purebasic.com/documentation/ ... tives.html

If you want values that are evaluated at Runtime, use variables.
User avatar
HeX0R
Addict
Addict
Posts: 1197
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Post by HeX0R »

Thx GedB for the wonderful explanation :!:

I had no idea, that pb handles it this way.
As i said, i know from other languages things like defined(CONSTANT) or similar to check if a Constant is already declared.
Now i know, why such a command is missing in pb :P

Then we just forget about the request
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post by GedB »

HeXOR,

I'm glad you found my explanation helpful. When I go back and read a post I always worry that my attempts at clarity and brevity just come across as impolite and arrogant. :?
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Post by freedimension »

GedB wrote:HeXOR,

I'm glad you found my explanation helpful. When I go back and read a post I always worry that my attempts at clarity and brevity just come across as impolite and arrogant. :?
LOL, i always think so about my posts ;-)

That's why I use so many smilies!
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

LOL, i always think so about my posts
That's why I use so many smilies!
Me too! :D :wink:
--Kale

Image
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

HeX0R wrote:As i said, i know from other languages things like defined(CONSTANT) or similar to check if a Constant is already declared.
Now i know, why such a command is missing in pb :P
I think this has been requested by some people (to check if a constant has been defined) - hopefully it will appear in the next version of PureBasic :)
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post by GedB »

I think this has been requested by some people (to check if a constant has been defined) - hopefully it will appear in the next version of PureBasic :)
Would that be as a Compiler Directive?
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

GedB wrote:
I think this has been requested by some people (to check if a constant has been defined) - hopefully it will appear in the next version of PureBasic :)
Would that be as a Compiler Directive?
It would have to be otherwise there would be no point in it. You wouldn't be able to do something like:

If defined(#MY_CONST)

at runtime, since all constants are replaced in the code with the value they represent. Chances are it would be done something like

CompilerIfDefined #MY_CONST

Although perhaps it would be more useful to have a general "defined" operator, so that you could use it on structures, variables (have they been specifically declared at this point in the code?), constants, etc. There must already be code for doing this since the compiler checks for stuff like that.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
GreenGiant
Enthusiast
Enthusiast
Posts: 252
Joined: Fri Feb 20, 2004 5:43 pm

Post by GreenGiant »

I think I've missed the point. What would be the advantage of a compiler directive to tell you whether a constant is already defined? Surely you could just run with debug on and it would tell you.
Post Reply