Why are constants global?

Everything else that doesn't fall into one of the other PB categories.
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Why are constants global?

Post by dell_jockey »

I just stumbled accross something I didn't know untill now: constants apparently have a global or at least a file-global scope. I can't declare constants with the same name in multiple procedures that reside in the same source file.

Why is that?

The help file says that constants are merely replaced by their value at compile time.
Why can't the compiler limit the scope of a constant to a procedure, when this constant was defined within that procedure?

If the above is a correct interpretation, please consider this a feature request.

Thanks!
Last edited by dell_jockey on Thu Oct 04, 2007 8:18 pm, edited 1 time in total.
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Constants would be very much less if they weren't accessible everywhere. Let's just imagine that all the standard constants, which are declared in a resident file, were not available anywhere outside that resident file.
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post by dell_jockey »

Trond wrote:Constants would be very much less if they weren't accessible everywhere. Let's just imagine that all the standard constants, which are declared in a resident file, were not available anywhere outside that resident file.
I'm not pleading for procedure-scope only constants. I'd like constants to be visible in the context of where they were defined.
- If defined in a file - outside a procedure - a constant should be visible everywhere within that file.
- If defined within a procedure, it should be visible only there. If within a procedure a constant name is reused that already has scope above the procedure level, is should be interpreted as a re-defintion, but only so within the scope of that procedure.
- constants in a resident file should of course remain global and non-redefinable within a procedure...

Just a thought....
Last edited by dell_jockey on Thu Oct 04, 2007 8:23 pm, edited 1 time in total.
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
Irene
Enthusiast
Enthusiast
Posts: 461
Joined: Thu Oct 04, 2007 12:41 pm

Post by Irene »

Why not just use a Static variable inside a Procedure?
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post by dell_jockey »

Irene wrote:Why not just use a Static variable inside a Procedure?
I'd rather not, because constants are immutable. This property would ensure that they will not be changed. It's an extra layer of security if you will...

Whatever, since that is not available right now, I'm indeed changing my constants to 'static doubles'. A kludgy alternative in my opinion, but so be it...
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

But constants that can be redefined, even within the context of local scope etc, are not constant!

Constants are imutable and thus it makes sense to have them globally scoped, at least in my opinion! :)
I may look like a mule, but I'm not a complete ass.
Irene
Enthusiast
Enthusiast
Posts: 461
Joined: Thu Oct 04, 2007 12:41 pm

Post by Irene »

srod wrote:at least in my opinion! :)
And that's what counts most doesn't it? ^_^
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

:?:
I may look like a mule, but I'm not a complete ass.
Irene
Enthusiast
Enthusiast
Posts: 461
Joined: Thu Oct 04, 2007 12:41 pm

Post by Irene »

srod wrote::?:
Just a little joke my friend @_@
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Ah, not familiar with the @_@ kind of symbolism I'm afraid.

All those years spent living in a tree I guess! :wink:
I may look like a mule, but I'm not a complete ass.
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

Anime/Manga emoticons, you'll get used to them. Look on the bright side, won't have to tilt your head to figure them out ;P
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

My 2 yen,

I would always assume constants to be global in any compiler I use. There are so many of them that usually they will be defined in other included source files. Think of all the constants for the win32api, these are not special or any different to ones you create yourself. It's not like these API ones are "Built in" constants which are global and "user defined" constants which are otherwise. There are just constants.

IMO, if constants have scope then what's constant about them?

If you write a module and create an include file, it would be a mess if you had to get people to define all your constants again in their procs.

Maybe I'm not understanding
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Post by Dr. Dri »

I think Fred talked about a module/endmodule keyword (for a future version) wich looked like a kind of namespace

Dri
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Post by electrochrisso »

I believe that all constants should be global just like they are for the overall operating system ones and that way only one namespace is assigned for it.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

me too.
a Constant is for defining something like #PI

there is no reason, why a constant should change it's value.

a constant is meant to be constant, like pi or e or Planck's constant...
they will stay the same whereever in the universe you may move to.

so, a namespace scope will completely negate the basic idea of a constant.
oh... and have a nice day.
Post Reply