Recent

Author Topic: FPC Feature request/suggestion  (Read 23861 times)

440bx

  • Hero Member
  • *****
  • Posts: 4993
FPC Feature request/suggestion
« on: April 24, 2020, 10:32:25 pm »
Hello,

If this has already been implemented in a version of FPC later than the one I'm using then, never mind. :)

When declaring variables, FPC accepts an initial value but, only if there is only one variable in the declaration.  i.e,
Code: Pascal  [Select][+][-]
  1. var
  2.   Avariable : integer = 0;  // this is ok, it compiles fine (v3.0.4)
but when there is more than one variable (of the same type of course) it doesn't accept an initial value.  i.e,
Code: Pascal  [Select][+][-]
  1. var
  2.   Avariable, AnotherVariable : integer = 0;  // does _not_ compile
it would be nice if the compiler would allow an initial value for multiple variables in the same declaration.  In this last case, the compiler complains that only one variable can be initialized.  It seems reasonable for the compiler to initialize them both to the same value.

Thank you for reading.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Otto

  • Full Member
  • ***
  • Posts: 226
Re: FPC Feature request/suggestion
« Reply #1 on: April 24, 2020, 10:49:26 pm »
Yours is a very good question, I asked myself a similar question.
I believe that for now we need to make an assignment for each declaration:

Code: Pascal  [Select][+][-]
  1. var
  2.       Avariable : integer = 0;
  3.       AnotherVariable : integer = 0;    
  4.  

Maybe with a little variation on the syntax of the FPC or a special mode of Lazarus we could have something like:

Code: Text  [Select][+][-]
  1.  var
  2.       Avariable:=0, AnotherVariable:=0 : integer;  
  3.  
« Last Edit: April 24, 2020, 10:57:50 pm by Otto »
Kind regards.

Bart

  • Hero Member
  • *****
  • Posts: 5510
    • Bart en Mariska's Webstek
Re: FPC Feature request/suggestion
« Reply #2 on: April 25, 2020, 01:05:07 pm »
Discussed many times before.

Bart

440bx

  • Hero Member
  • *****
  • Posts: 4993
Re: FPC Feature request/suggestion
« Reply #3 on: April 25, 2020, 02:04:08 pm »
Discussed many times before.

Bart
Ok.  Any particular reason you know of why it has not been implemented ?
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12028
  • FPC developer.
Re: FPC Feature request/suggestion
« Reply #4 on: April 25, 2020, 02:05:42 pm »
Yeah, and allow a:=b:=c:=d=e;

statements too, to set a,b,c to the result of the comparison of d and e  >:D

440bx

  • Hero Member
  • *****
  • Posts: 4993
Re: FPC Feature request/suggestion
« Reply #5 on: April 25, 2020, 02:12:55 pm »
Yeah, and allow a:=b:=c:=d=e;

statements too, to set a,b,c to the result of the comparison of d and e  >:D
I'm not asking for/suggesting any of those things.  Using one value to initialize a group of variables is a very minor change to the compiler.  It already handles groups of variables in declarations and it already allows initializing one variable.  "Joining" those two, already present, abilities seems logical.

At this point, I'm just curious to find out if there is a _genuinely_ good reason not to implement that capability.
« Last Edit: April 25, 2020, 02:20:05 pm by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10792
  • Debugger - SynEdit - and more
    • wiki

BeniBela

  • Hero Member
  • *****
  • Posts: 921
    • homepage
Re: FPC Feature request/suggestion
« Reply #7 on: April 25, 2020, 02:49:32 pm »
Constants can be declared with:

Code: Pascal  [Select][+][-]
  1.     const
  2.       Avariable = 0;
  3.       AnotherVariable  = 0;  
  4.  

You do not need comma, when you already can use semicolons.

Looks like the best way, would be to allow the const syntax for variables:


Code: Pascal  [Select][+][-]
  1.     var
  2.       Avariable = 0;
  3.       AnotherVariable = 0;  
  4.  

Otto

  • Full Member
  • ***
  • Posts: 226
Re: FPC Feature request/suggestion
« Reply #8 on: April 25, 2020, 03:06:31 pm »
[...]
Looks like the best way, would be to allow the const syntax for variables:


Code: Pascal  [Select][+][-]
  1.     var
  2.       Avariable = 0;
  3.       AnotherVariable = 0;  
  4.  

For this to be possible, the compiler should infer the type based on the value attributed to the variable, even in the variable declaration. Controversial cases could arise.
Kind regards.

BeniBela

  • Hero Member
  • *****
  • Posts: 921
    • homepage
Re: FPC Feature request/suggestion
« Reply #9 on: April 25, 2020, 03:18:42 pm »
[...]
Looks like the best way, would be to allow the const syntax for variables:


Code: Pascal  [Select][+][-]
  1.     var
  2.       Avariable = 0;
  3.       AnotherVariable = 0;  
  4.  

For this to be possible, the compiler should infer the type based on the value attributed to the variable, even in the variable declaration. Controversial cases could arise.

Well, since the compiler can already do that in the const section, there is no difference.

Although this
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. const a = 0;
  3. begin
  4.   writeln(sizeof(a));
  5. end.
  6.  
prints 1.

So it infers the type to be byte. That is indeed controversial

440bx

  • Hero Member
  • *****
  • Posts: 4993
Re: FPC Feature request/suggestion
« Reply #10 on: April 25, 2020, 03:21:44 pm »
and maybe others
I see discussions but, I don't see any technical reasons there.

One of the reasons that feature should be implemented is for symmetry.  The compiler allows initializing one variable, i.e,
Code: Pascal  [Select][+][-]
  1. var
  2.   SomeVar : integer = 0;

It really gives a poor impression that the compiler is "challenged" to initialize more than one variable.

I don't even think of that as a feature, just polish.  Something the compiler should be able to do just for symmetry and completeness.


(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5855
  • Compiler Developer
Re: FPC Feature request/suggestion
« Reply #11 on: April 25, 2020, 03:47:59 pm »
Maybe with a little variation on the syntax of the FPC or a special mode of Lazarus we could have something like:

Code: Text  [Select][+][-]
  1.  var
  2.       Avariable:=0, AnotherVariable:=0 : integer;  
  3.  

Definitely no. The compiler needs to know the type first. Thus 440bx's suggestion would technically be possible, yours would not.

440bx

  • Hero Member
  • *****
  • Posts: 4993
Re: FPC Feature request/suggestion
« Reply #12 on: April 25, 2020, 03:55:19 pm »
Thus 440bx's suggestion would technically be possible, yours would not.
I'm pleased you acknowledge it is technically possible.  Is it an idea you would consider implementing ?  If your answer is "no", I'd like to know why.   Thank you. :)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5855
  • Compiler Developer
Re: FPC Feature request/suggestion
« Reply #13 on: April 25, 2020, 05:51:38 pm »
After thinking about it a bit, I can say that I wouldn't consider it. Let me explain my reasoning:

While in a declaration like this it might be rather clear:

Code: Pascal  [Select][+][-]
  1. var
  2.   a, b, c, d, e, f, g, h, i, j, k : Integer = 255;

The initialization will become rather lost if you have longer variable names and spread them across multiple lines (this coding style is used in the compiler for example):

Code: Pascal  [Select][+][-]
  1. var
  2.   SomeVariableName1,
  3.   SomeVariableName2,
  4.   SomeVariableName3,
  5.   SomeVariableName4,
  6.   SomeVariableName5,
  7.   SomeVariableName6,
  8.   SomeVariableName7,
  9.   SomeVariableName8,
  10.   SomeVariableName9,
  11.   SomeVariableName10: Integer = 255;

When looking at such code you might not notice that SomeVariableName1 is initialized to some value.
I grant you that this might not make a difference when a new variable is added, cause if you don't realize that it's already initialized you'll just initialize it yourself (and the compiler could optimize away the initialization from the var section).
When refactoring however or changing the type of a single variable this might however be significant (we've all had days where we thought we should have noticed that pesky = 255 at the end (and we might even have), but for some reason we forgot it (e.g. someone distracted you or whatever) and then we had a lengthy debugging season on our hands).

440bx

  • Hero Member
  • *****
  • Posts: 4993
Re: FPC Feature request/suggestion
« Reply #14 on: April 25, 2020, 06:05:39 pm »
The initialization will become rather lost if you have longer variable names and spread them across multiple lines (this coding style is used in the compiler for example):

Code: Pascal  [Select][+][-]
  1. var
  2.   SomeVariableName1,
  3.   SomeVariableName2,
  4.   SomeVariableName3,
  5.   SomeVariableName4,
  6.   SomeVariableName5,
  7.   SomeVariableName6,
  8.   SomeVariableName7,
  9.   SomeVariableName8,
  10.   SomeVariableName9,
  11.   SomeVariableName10: Integer = 255;

When looking at such code you might not notice that SomeVariableName1 is initialized to some value.
The only way I can think of for SomeVariableName1 to get lost is if the programmer doesn't even bother to find out what type the variable is.  The fact that every variable is followed by a comma, I'd say forces the programmer to keep reading down until finding the data type which is immediately followed by the value.  The initial value can't be "lost" without also "losing" the variable's data type.

When refactoring however or changing the type of a single variable this might however be significant (we've all had days where we thought we should have noticed that pesky = 255 at the end (and we might even have), but for some reason we forgot it (e.g. someone distracted you or whatever) and then we had a lengthy debugging season on our hands).
As stated above, that "pesky" = 255 is right next to the equally "pesky" data type.  All that said, I have to admit that if the programmer isn't paying to data types, it is quite likely that the debugger will get a good workout.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018