Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2006-10

typed constants...

6 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

David Mears

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.

Reply by email

Michael Van Canneyt
to David Mears
David Mears wrote 15 lines
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.

Reply by email

Jonas Maebe
to Michael Van Canneyt
Michael Van Canneyt wrote 8 lines
"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($80000000)) or for overload selection.

Jonas

Reply by email

David Mears
to Michael Van Canneyt: Initialized constants are deprecated, and should be replaced by initialized variables, as…
Michael Van Canneyt wrote 41 lines
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 - 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. :)

Reply by email

Marc Santhoff
to David Mears
David Mears wrote 36 lines
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. >
Show quoted text 22 lines
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

Reply by email