Re: [fpc-pascal] Typed Constants vs. Variables
On 06 Apr 2009, at 03:37, Richard Ward wrote: Where/why would one use a typed constant vs. a variable. i.e. const myConst : double = 2.0*Pi; var myVar : double = 2.0*Pi; It seems to me the typed constant is superfluous and can potentially lead to bugs. Typed constants predate the existence of initialized variables. Furthermore, a) typed constants are initialized once (when the program starts), and if changed keep their value until explicitly changed again (even if they are declared locally in a procedure/function, and over multiple independent invocations of such routines) b) conversely, initialized variables are initialized every time their scope is activated (or whatever the proper term for that is: once in case they are declared in a program/unit scope, and every time a function/procedure is entered if they are declared locally in a routine) c) typed constants can be made read-only by using the {$j-} switch Jonas ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Typed Constants vs. Variables
Jonas wrote: b) conversely, initialized variables are initialized every time their scope is activated (or whatever the proper term for that is: once in case they are declared in a program/unit scope, and every time a function/procedure is entered if they are declared locally in a routine) --- Thanks for this explicit clarification. This was the bit of information which was not getting through to me before. Francisco, In FPC, there are two types of constants: ordinary and typed. Ordinary constants are what you were thinking of while typed constants act a bit differently as Jonas and lele wrote. My confusion was that typed constants and initialized variables seemed to me to behave exactly the same - that is, acted like variables - until I learned the subtle difference in (b) above. Although I can appreciate why the term typed constant was selected for such a language feature, it was confusing to me since I tend to think of constants as being constant and one has to be careful in realizing the difference lest things don't work the way as one might expect. - ROW ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
[fpc-pascal] Typed Constants vs. Variables
Where/why would one use a typed constant vs. a variable. i.e. const myConst : double = 2.0*Pi; var myVar : double = 2.0*Pi; It seems to me the typed constant is superfluous and can potentially lead to bugs. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Typed Constants vs. Variables
Richard Ward writes: Where/why would one use a typed constant vs. a variable. i.e. Any time you have a value you want to have in an accessible holder, but want to ensure you will not change it's value by mistake. It seems to me the typed constant is superfluous and can potentially lead to bugs. On the contrary. They help saveguards against bugs. Imagine a team of people working on different parts of a large system. One team defines a constant; someone on another team sees a piece of code and thinkgs he working with a variable. He tries to change it and the compiler throws an error because it is a constant. That error just saved that system from a logical, possibly hard to find, error. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Typed Constants vs. Variables
Typed constants ARE changeable (just like variables), the only place where they behave differently from variables is if they're declared in a local scope (i.e. function / procedure). Typed constants act like a static variables in C, where it's initialized only once during program execution. -- View this message in context: http://www.nabble.com/Re%3A-Case-Sensitivity---Multiple-Names-tp22897965p22902217.html Sent from the Free Pascal - General mailing list archive at Nabble.com. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] typed constants...
Am Montag, den 23.10.2006, 20:28 -0400 schrieb David Mears: Michael Van Canneyt wrote: On Mon, 23 Oct 2006, David Mears wrote: is this bit of syntactic weirdness a fpc element, or from delphi. I've largely been away from pascal since the early 90s, mostly only using it to write dos-y non-object-y utilities, for which it excels. typed constants seem to basically to be like the static keyword from c.. but not a var modifier.. It just seems it should be a modifier for var. such as var st:string static;.. since.. constant is usually pretty wrapped up in the meaning of not changing. and that it has a constant, initialized, reserved place in memory is.. well.. abstract. Especially since you can initialize your variables now, then the only thing that makes it special is that it is a global variable with a local scope. I'm not the sort who thinks pascal should be C, because I hate having to work with C or it's work likes. I just think that being able to call something constant and change it muggles the syntactic clarity of the language, which is otherwise rather good. Initialized constants are deprecated, and should be replaced by initialized variables, as in Delphi: Var A : String = 'Some string'; Real constants (in the sense of 'not changing') do not need to be typed in the first place so Const A = 'Some String'; Will do just fine. You now have both possibilities and they each have clear and unambiguous meaning. I was less interested in the initialized part- which I'm happy you can do with variables now but would not be overly concerned setting them in code as I always had to in the past, as the It should be stressed that typed constants are initialized at program start. This is also true of local typed constants. Which I take it to mean that they retain their value when out of scope, which provides encapsulation at a level that can only otherwise be done with objects. like function HowManyBirds:integer; const n: integer = 1; begin n:=n * 2; end; ignoring that the function here is useless, that's a useful ability.. while you can define variables anywhere not in procedure, nothing else quite does that. It's just I'd rather it be var n: integer = 1; static; var n: integer = 1; fixed; or some such. It makes more sense linguistically, in an otherwise very sensible language. :) Why don't you just type and initialize your constants? I'm doing it all the time like this (from a real world working program): const H5FILE_NAME : string = 'SDS_P.h5'; DATASETNAME : string = 'IntArray'; That should do what you want IIRC, Marc ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
[fpc-pascal] typed constants...
is this bit of syntactic weirdness a fpc element, or from delphi. I've largely been away from pascal since the early 90s, mostly only using it to write dos-y non-object-y utilities, for which it excels. typed constants seem to basically to be like the static keyword from c.. but not a var modifier.. It just seems it should be a modifier for var. such as var st:string static;.. since.. constant is usually pretty wrapped up in the meaning of not changing. and that it has a constant, initialized, reserved place in memory is.. well.. abstract. Especially since you can initialize your variables now, then the only thing that makes it special is that it is a global variable with a local scope. I'm not the sort who thinks pascal should be C, because I hate having to work with C or it's work likes. I just think that being able to call something constant and change it muggles the syntactic clarity of the language, which is otherwise rather good. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] typed constants...
On Mon, 23 Oct 2006, David Mears wrote: is this bit of syntactic weirdness a fpc element, or from delphi. I've largely been away from pascal since the early 90s, mostly only using it to write dos-y non-object-y utilities, for which it excels. typed constants seem to basically to be like the static keyword from c.. but not a var modifier.. It just seems it should be a modifier for var. such as var st:string static;.. since.. constant is usually pretty wrapped up in the meaning of not changing. and that it has a constant, initialized, reserved place in memory is.. well.. abstract. Especially since you can initialize your variables now, then the only thing that makes it special is that it is a global variable with a local scope. I'm not the sort who thinks pascal should be C, because I hate having to work with C or it's work likes. I just think that being able to call something constant and change it muggles the syntactic clarity of the language, which is otherwise rather good. Initialized constants are deprecated, and should be replaced by initialized variables, as in Delphi: Var A : String = 'Some string'; Real constants (in the sense of 'not changing') do not need to be typed in the first place so Const A = 'Some String'; Will do just fine. You now have both possibilities and they each have clear and unambiguous meaning. Michael. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] typed constants...
On 23 Oct 2006, at 19:30, Michael Van Canneyt wrote: Real constants (in the sense of 'not changing') do not need to be typed in the first place so Const A = 'Some String'; Will do just fine. You can also give them a type by simply adding a typecast around the value. This can be necessary sometimes for range checking (e.g. longint($8000)) or for overload selection. Jonas ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] typed constants...
Michael Van Canneyt wrote: Initialized constants are deprecated, and should be replaced by initialized variables, as in Delphi: They are? Why? They have the practical advantage of being able to share them with other instances of this process. At least, if compiler+linker cooperate to put them in the right section. Micha ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] typed constants...
Michael Van Canneyt wrote: On Mon, 23 Oct 2006, David Mears wrote: is this bit of syntactic weirdness a fpc element, or from delphi. I've largely been away from pascal since the early 90s, mostly only using it to write dos-y non-object-y utilities, for which it excels. typed constants seem to basically to be like the static keyword from c.. but not a var modifier.. It just seems it should be a modifier for var. such as var st:string static;.. since.. constant is usually pretty wrapped up in the meaning of not changing. and that it has a constant, initialized, reserved place in memory is.. well.. abstract. Especially since you can initialize your variables now, then the only thing that makes it special is that it is a global variable with a local scope. I'm not the sort who thinks pascal should be C, because I hate having to work with C or it's work likes. I just think that being able to call something constant and change it muggles the syntactic clarity of the language, which is otherwise rather good. Initialized constants are deprecated, and should be replaced by initialized variables, as in Delphi: Var A : String = 'Some string'; Real constants (in the sense of 'not changing') do not need to be typed in the first place so Const A = 'Some String'; Will do just fine. You now have both possibilities and they each have clear and unambiguous meaning. Michael. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal I was less interested in the initialized part- which I'm happy you can do with variables now but would not be overly concerned setting them in code as I always had to in the past, as the It should be stressed that typed constants are initialized at program start. This is also true of local typed constants. Which I take it to mean that they retain their value when out of scope, which provides encapsulation at a level that can only otherwise be done with objects. like function HowManyBirds:integer; const n: integer = 1; begin n:=n * 2; end; ignoring that the function here is useless, that's a useful ability.. while you can define variables anywhere not in procedure, nothing else quite does that. It's just I'd rather it be var n: integer = 1; static; var n: integer = 1; fixed; or some such. It makes more sense linguistically, in an otherwise very sensible language. :) ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal