Initialize constants from variables ?

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Andre.

Maybe its not allowed or possible. But it would be nice, if I can initialize some constants (at top of my source - maybe this as restriction) from a variable.

Something like:

; Initialize gadget IDs
a=0
#ReadText=a : a+1
#ReadString=a : a+1
#ReadReq=a : a+1
#CallListView=a : a+1
#SaveText=a : a+1
#SaveString=a : a+1
#SaveReq=a : a+1
#ListView=a : a+1
#SaveOption=a : a+2 ; add "2" because there are two SaveOption's

Thanks!

André
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.
Maybe its not allowed or possible. But it would be nice, if I can initialize some constants (at top of my source - maybe this as restriction) from a variable.
I tried this (it worked in AmiBlitz), but it does not work in PB so I guess you cannot change the value of a constant once it has been defined. I doubt you'd be able to initialise the constants from variables, because their values need to be known when the code is compiled.

Code: Select all

; Enum test

#ENUM=0
#ReadText=#ENUM      : #ENUM=#ENUM+1
#ReadString=#ENUM    : #ENUM=#ENUM+1
#ReadReq=#ENUM       : #ENUM=#ENUM+1
#CallListView=#ENUM  : #ENUM=#ENUM+1
#SaveText=#ENUM      : #ENUM=#ENUM+1

; Check that it works :)
If OpenConsole()
	PrintN(Str(#ReadText))
	PrintN(Str(#ReadString))
	PrintN(Str(#ReadReq))
	PrintN(Str(#CallListView))
	PrintN(Str(#SaveText))
	Input()
	CloseConsole()
EndIf
End
Perhaps a C style "enum" command would be useful?
(And macros :)


--
It's not minimalist - I'm increasing efficiency by reducing input effort.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.

I had the same question last year and got the answer, that it is not allowed to declare a constant from the value of a variable.
And that you can't change the value of a constant because it is a constant and not a variable.
This makes sense.
Once declared you can't change it on runtime.
Otherwise it would be a variable.


Have a nice day...
Franco

Sometimes you have to go a lonely way to accomplish genius things.

Edited by - franco on 07 April 2002 18:51:31
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> it would be nice, if I can initialize some constants [...] from a variable.
> Something like: #ReadText=a : a+1 : #ReadString=a

Isn't this already possible? I thought it was (can't test it right now because
I'm at work without my laptop). If not, then I agree: it should theoretically
be possible because you're assigning the current value of the variable to the
constant. That is, if a=123 then PureBasic should just take this value and assign
it just as if you had used #constant=123. I can't see any technical reason why
it can't be done...


PB - Registered PureBasic Coder

Edited by - PB on 07 April 2002 20:20:24
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Andre.
Isn't this already possible? I thought it was (can't test it right now because
I'm at work without my laptop). If not, then I agree: it should theoretically
be possible because you're assigning the current value of the variable to the
constant. That is, if a=123 then PureBasic should just take this value and assign it just as if you had used #constant=123. I can't see any technical reason why it can't be done...
No, it isn't possible. It gives an error requester.

It was only an idea from me. Its not so important but would be a nice addition.
If declaring constants from variables inside the regular program code isn't possible, it would be nice to be able to use this inside a "Declaring section" at the start of the code...


Regards
André

*** German PureBasic Support ***
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

It's not possible because a constant is like a numeric alias. The value is filled at COMPILE time, not at EXECUTION time. But I could do the tinman solution (ie: accept than a constant change its constant value when compiling).


Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

Constants are Constants. They never change.

Why not using a variable when you want the value changed ??

I think Franco is right:
>And that you can't change the value of a constant because
>it is a constant and not a variable.
>This makes sense.
>Once declared you can't change it on runtime.
>Otherwise it would be a variable.

Changing Constants isnt logical.
It would be nearly the same as
Variables.

ATM you know what the value of a constant is.
If you can change it, it could give you
some trouble finding a bug later, because the
constant is changed somewhere in your big source.

When you cant change the constant you know
the right value of it at everytime in your source.
Thats logical. Thats how it is now.

Simply use Variables for not-fixed values.

cya,
...Danilo

(registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Stan.

Hi,
...
; Initialize gadget IDs
a=0
#ReadText=a : a+1
#ReadString=a : a+1
#ReadReq=a : a+1
#CallListView=a : a+1
#SaveText=a : a+1
#SaveString=a : a+1
#SaveReq=a : a+1
#ListView=a : a+1
#SaveOption=a : a+2 ; add "2" because there are two SaveOption's
...
That would be possible to do even with a single pass compiler (implementing
constant by reference would do the trick ... ), BUT this would imply an
efficiency "penalty" for all the people who use constant as they were
intended to be used ... If I understand you well, you need to have "constants"
which you can reasonably assume to have a given value at some stage
(semiconstant?), if so a possible solution could be :

Code: Select all

 
...
; Initialize gadget IDs
a=0
CST_ReadText=a      : a+1
CST_ReadString=a    : a+1
CST_ReadReq=a       : a+1
CST_CallListView=a  : a+1
CST_SaveText=a      : a+1
CST_SaveString=a    : a+1
CST_SaveReq=a       : a+1
CST_ListView=a      : a+1
CST_SaveOption=a    : a+2   ; add "2" because there are two SaveOption's
...


Obviously these are variables but it would remind you they are supposed to be
constant.

If what you really want are real constant (in the PB sense), but want to be
able to change their values with a minimum of effort, then your best bet is
to use a good text editor which can do that kind of trick (Ultra Edit for
instance).

Hope this help.

Stan


Since I attended an MS course, my programs no longer have bugs ... just hidden "features" !! [ PB. registered user ]
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> The value is filled at COMPILE time, not at EXECUTION time.

A very good point that I keep forgetting!


PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Andre.
; Initialize gadget IDs
a=0
CST_ReadText=a : a+1
CST_ReadString=a : a+1
CST_ReadReq=a : a+1
CST_CallListView=a : a+1
CST_SaveText=a : a+1
CST_SaveString=a : a+1
CST_SaveReq=a : a+1
CST_ListView=a : a+1
CST_SaveOption=a : a+2 ; add "2" because there are two SaveOption's...

Obviously these are variables but it would remind you they are supposed to be
constant.
Its a good idea.

I wanted to use the "changable" constants because I would like to have automatically created constant values, but also want be able to change the sorting of the constants without changing their value by hand... :wink:


Regards
André

*** German PureBasic Support ***
Post Reply