Why are constants global?
-
dell_jockey
- Enthusiast

- Posts: 767
- Joined: Sat Jan 24, 2004 6:56 pm
Why are constants global?
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!
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.
-
dell_jockey
- Enthusiast

- Posts: 767
- Joined: Sat Jan 24, 2004 6:56 pm
I'm not pleading for procedure-scope only constants. I'd like constants to be visible in the context of where they were defined.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.
- 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.
-
dell_jockey
- Enthusiast

- Posts: 767
- Joined: Sat Jan 24, 2004 6:56 pm
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...Irene wrote:Why not just use a Static variable inside a Procedure?
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...
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
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
“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
- electrochrisso
- Addict

- Posts: 989
- Joined: Mon May 14, 2007 2:13 am
- Location: Darling River
- Kaeru Gaman
- Addict

- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany
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.
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.

