[Implemented] Str() for Constants

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Dummy
Enthusiast
Enthusiast
Posts: 162
Joined: Wed Jun 09, 2004 11:10 am
Location: Germany
Contact:

[Implemented] Str() for Constants

Post by Dummy »

It would be nice if the following Code would work:

Code: Select all

#Version = "MyApp v0.01 BUILD " + Str(#JaPBe_ExecuteBuild)
If you don't use JaPBe you have to add this line at the beginning:

Code: Select all

#JaPBe_ExecuteBuild = 1
Lebostein
Addict
Addict
Posts: 826
Joined: Fri Jun 11, 2004 7:07 am

Post by Lebostein »

You can't create a constant with a Function! Str() is no problem. You can use a variable:

Code: Select all

Version$ = "MyApp v0.01 BUILD " + Str(#JaPBe_ExecuteBuild)
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Post by freedimension »

I'm pretty sure he knows that.

But go and try this

Code: Select all

#wowImConstant = "lalala"+Chr(13)+"dipsy"
It's quite the same and does work, or?
That's because Chr is checked at compiletime if found inside a constant-declaration, and that's what he's asking for the str()-command.
Lebostein
Addict
Addict
Posts: 826
Joined: Fri Jun 11, 2004 7:07 am

Post by Lebostein »

Wow! Sorry for my blindfold post, Chr() is also a function... :oops:


But try this: :D

Code: Select all

test = 13
#wowImConstant = "lalala" + Chr(test) + "dipsy"
Debug #wowImConstant
PureBasic crashes...
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

that.... is a bug...
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Just to clarify, for my own understanding:

Anything that requires compilation to determine the value assigned to a constant is basically illegal (that is what variables are for). Otherwise PB would need to run the program as it was compiling it.

Code: Select all

compileTimeSec.s = Chr(Second(Date()))
#_compiledOn = "Compiled on this second: "+compileTimeSec+"!"
So the crash reported is a bug in as much as PB doesn't report use of a variable where it shouldn't be used - it is not a bug in that PB should allow a variable there, because variables vary and are not, um constant.

hehe. :)

Is this what you mean? Thanks.
@}--`--,-- A rose by any other name ..
Lebostein
Addict
Addict
Posts: 826
Joined: Fri Jun 11, 2004 7:07 am

Post by Lebostein »

Look that:

Code: Select all

For i = 1 To 20

compileTimeSec.s = Str(ElapsedMilliseconds())
#_compiledOn = "Compiled on this second: " + compileTimeSec + "!"
Debug #_compiledOn

Next i
What is this? The Debuger have no problem with this. Why?
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Lebostein wrote:What is this?
lol. Good Q.

So what are we wishing for/requesting here?
  • 1: Constants can only use literals or previously defined constants when assigning values.
    2: Simple functions/operations such as Chr, Str, +, - and etc allowed.
    3: Compiler has a hissy and spits out an error message otherwise.
If so, I agree, I would like to see this.

I wish I may, I wish I might
Get constant definitions right!
@}--`--,-- A rose by any other name ..
ivory
User
User
Posts: 36
Joined: Fri Jun 25, 2004 2:30 am

Post by ivory »

It seems to me, that it's not really the function thats in question, but the parameter.

#myconst = "text" + chr(13) + "more text"
This example passes a constant 13 to the function, and could be interpreted by the compiler with ease.

CR.L = 13
#myconst = "text" + chr(CR) + "more text"
This example passes a variable and can not be determined at compile time.

#myconst = "text" + chr(#myotherconst) + "more text"
This example also passes a constant, in theory, and since the compiler does not allow a constant to be defined multiple times, and it does not allow variables to be assigned to constants (since variables are run time only) then this should work. But remember, either the constant has to be defined earlier in the source code, or the compiler needs to catch it on a 1st pass (I don't know if pure is 1 or 2 pass compiler).

Many of the exotic examples in various posts of this thread just aren't realistic because they try to put run time elements into a constant, which defeats the purpose.

Added:
Having said all that, it should not matter whether the function is CHR or STR or ASC or anyother function, as long both the function and the paramter are pre-defined and not variable. Obviously any function to be used in this way would need to be executable by the compiler.

I would also like to add that there are a number of special case variables which could be added as pseudo constants. Date, Time, Source File Name, path, object name, user name, build#, etc. Being able to inject these items into a constant with something like a format$ function could be useful for project control.
Post Reply