Page 1 of 2
More comfortable Constant handling...
Posted: Fri Sep 24, 2004 11:05 am
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 :
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.
Posted: Fri Sep 24, 2004 11:18 am
by freedimension
look the word "constant" up in a dictionary
What you need is a variable
Oh well, I looked it up for you
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.
Posted: Fri Sep 24, 2004 11:36 am
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 ?
Posted: Fri Sep 24, 2004 12:24 pm
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.
Re: More comfortable Constant handling...
Posted: Fri Sep 24, 2004 12:34 pm
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?
Posted: Fri Sep 24, 2004 12:38 pm
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!
Posted: Fri Sep 24, 2004 1:24 pm
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.
Posted: Fri Sep 24, 2004 2:26 pm
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
Then we just forget about the request
Posted: Fri Sep 24, 2004 3:47 pm
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.

Posted: Fri Sep 24, 2004 4:19 pm
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!
Posted: Fri Sep 24, 2004 4:28 pm
by Kale
LOL, i always think so about my posts
That's why I use so many smilies!
Me too!

Posted: Fri Sep 24, 2004 4:47 pm
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 :)
Posted: Fri Sep 24, 2004 5:26 pm
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?
Posted: Fri Sep 24, 2004 6:17 pm
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.
Posted: Fri Sep 24, 2004 7:26 pm
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.