Recent

Author Topic: Constants declaration  (Read 7279 times)

mrguzgog

  • Jr. Member
  • **
  • Posts: 71
Constants declaration
« on: January 19, 2018, 03:19:30 pm »
Is there particular reason why constants cannot be declared using ',' as a separator, eg

Code: [Select]
const A=1, B=2, C=3;


As far as I can see the '=' and ',' symbols can't be used as identifiers so that isn't the issue. I know I can use the following instead, but it just seems odd when variables can be declared with comma seperators.
Code: [Select]
const a=1; B=2; C=3;

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Constants declaration
« Reply #1 on: January 19, 2018, 03:31:09 pm »
The chosen solution is to separate using ";",  there are about 50 other ascii characters not allowed in identifiers that  can also not be used to aggregate constants.


mrguzgog

  • Jr. Member
  • **
  • Posts: 71
Re: Constants declaration
« Reply #2 on: January 19, 2018, 07:40:25 pm »
The chosen solution is to separate using ";",  there are about 50 other ascii characters not allowed in identifiers that  can also not be used to aggregate constants.

Yes but I was asking if there was a particular reason - it seems a bit incongruous that variables can be separated by commas when declared but constants cannot, even if they're of the same type. It's just easier on the eye with commas IMHO.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Constants declaration
« Reply #3 on: January 19, 2018, 07:52:20 pm »
Yes but I was asking if there was a particular reason - it seems a bit incongruous that variables can be separated by commas when declared but constants cannot, even if they're of the same type. It's just easier on the eye with commas IMHO.

Yes. variables have a shortcut, but only when not initialized. And constants are per definition initialized :)

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Constants declaration
« Reply #4 on: January 19, 2018, 08:05:54 pm »
It's just easier on the eye with commas IMHO.

Yes, I agree. But I usually choose good names them and write each of them in their own line, so it will look good. For example this is the constants declaration of my "Furious Paladin":

Code: Pascal  [Select][+][-]
  1. const
  2.   ScreenWidth     = 500;
  3.   ScreenHeight    = 300;
  4.   MaxPlayerHealth = 500;
  5.   PlayerHealthInc = 0.08;
  6.   EnemyStrength   = 140;  

I don't think the code above will look good using comma, nor write them all in a single line.

mrguzgog

  • Jr. Member
  • **
  • Posts: 71
Re: Constants declaration
« Reply #5 on: January 20, 2018, 09:27:31 am »
Yes but I was asking if there was a particular reason - it seems a bit incongruous that variables can be separated by commas when declared but constants cannot, even if they're of the same type. It's just easier on the eye with commas IMHO.

Yes. variables have a shortcut, but only when not initialized. And constants are per definition initialized :)
OK but that begs the question as to why variables don't have a short-cut when initialised (and there's no inference required). I guess it's either just history/standards or something to do with the way the compiler works and if it's the latter I probably wouldn't understand!

@Handoko: Yes, I'd use that format in Pascal or any other language I've used except when initialising a group of consts with short names - in my current project I have some consts for the cardinal directions which form a sensible group and would take up less room if this was possible:

Code: [Select]
const
    N=1, NE=2, E=4, SE=8, S=16 ...

rather than having them on eight lines or punctuated with ';', which to me denotes the end of a statement, but that's just personal preference.

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Constants declaration
« Reply #6 on: January 20, 2018, 10:45:53 am »
; is used to separate expressions. Each assignment is a separate expression.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Constants declaration
« Reply #7 on: January 20, 2018, 12:04:04 pm »
OK but that begs the question as to why variables don't have a short-cut when initialised (and there's no inference required).

Because the variable shortcut is original 45 year old pascal, and the initialization is a D4 Visual Basic me-too feature that isn't terribly well designed.

But mistakes in the past are no reason to pile up more. A language is not a graffiti wall, where everybody must leave behind a piece of own invented syntax. Less is more.
« Last Edit: January 20, 2018, 12:36:05 pm by marcov »

Thaddy

  • Hero Member
  • *****
  • Posts: 14203
  • Probably until I exterminate Putin.
Re: Constants declaration
« Reply #8 on: January 20, 2018, 12:23:33 pm »
; is used to separate expressions. Each assignment is a separate expression.

Indeed each assignment is even a complete expression and therefor must be denoted by a semicolon. That's simply Pascal syntax  rules.
In the case of const an assignment must be part of the declaration and that is also Pascal syntax rules.

Defaults in parameters and enums are different: that is like Marco said.
Specialize a type, not a var.

mrguzgog

  • Jr. Member
  • **
  • Posts: 71
Re: Constants declaration
« Reply #9 on: January 20, 2018, 01:29:16 pm »
; is used to separate expressions. Each assignment is a separate expression.
That sounds reasonable and explains why 'var a, b, c;'  is at least different - 'b' on its own is not an expression.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Constants declaration
« Reply #10 on: January 20, 2018, 01:36:12 pm »
At the risk of sounding pedantic I would say (rather than being an expression) that each const assignment is a separate statement, and that each Pascal statement requires a semicolon separator.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Constants declaration
« Reply #11 on: January 20, 2018, 01:41:58 pm »
At the risk of sounding pedantic I would say (rather than being an expression) that each const assignment is a separate statement, and that each Pascal statement requires a semicolon separator.

Yes, but as OP says, the same could be said for

Code: Pascal  [Select][+][-]
  1. var a, b : Integer;

vs

Code: Pascal  [Select][+][-]
  1. var a : integer;
  2.      b: integer;

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Constants declaration
« Reply #12 on: January 20, 2018, 04:00:01 pm »
as said before,. they are all separate statements..

but, like shown here. Var one, two, three:Integer;

 That is also a single statement only because they all point to the same type, so this
makes perfect sense.

 If for example you would like to see a series of variables set to the same value, then would this
make perfect sense ..

 One, two, three := 1;

 and only on cases like this, just like when declaring a series of variables with the same type.

The only true wisdom is knowing you know nothing

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Constants declaration
« Reply #13 on: January 20, 2018, 04:26:57 pm »
At the risk of sounding pedantic I would say (rather than being an expression) that each const assignment is a separate statement, and that each Pascal statement requires a semicolon separator.

Yes, but as OP says, the same could be said for

Code: Pascal  [Select][+][-]
  1. var a, b : Integer;

vs

Code: Pascal  [Select][+][-]
  1. var a : integer;
  2.      b: integer;

Only by ignoring the initialization requirement which the syntax for const declarations involves.
Code: Pascal  [Select][+][-]
  1. var a, b: Integer;
is a single declaration statement, allocating memory for several identically typed variables using the short-hand syntax for the sequence of names.

Whereas
Code: Pascal  [Select][+][-]
  1. const N=1;
is already a complete Pascal statement, both allocating memory for the constant, naming it and initializing its value.
To allocate memory, name and initialize a further constant requires a further statement, separate from the first.

Just as
Code: Pascal  [Select][+][-]
  1. var a: Integer = 80;
is a complete Pascal statement, and further initialized variables need to be declared in further statements separated from the first by a semicolon.

isidroco

  • New Member
  • *
  • Posts: 12
Re: Constants declaration
« Reply #14 on: March 01, 2018, 01:31:24 am »

Code: [Select]
const
    N=1, NE=2, E=4, SE=8, S=16 ...

rather than having them on eight lines or punctuated with ';', which to me denotes the end of a statement, but that's just personal preference.

You can group them together all in one line with ';' even if each assignation is a statement and it's quite legible if constants are from a similar group (ie: North=0; East=90; South=180; West=270;)

 

TinyPortal © 2005-2018