[fpc-devel] Multiple variable initialization
Ondrej Pokorny
lazarus at kluug.net
Sat Mar 24 17:54:27 CET 2018
On 24.03.2018 16:53, Jonas Maebe wrote:
> You'd also have to support it at least for record field initialisers
and default parameters.
Oh yes, I'd also love to write
procedure Test(const A, B, C, D: Integer = 0);
instead of
procedure Test(const A: Integer = 0; const B: Integer = 0; const C:
Integer = 0; const D: Integer = 0);
On 24.03.2018 16:53, Jonas Maebe wrote:
> In the end, pretty much every extra language feature makes both the
> compiler and the language more complex. Therefore, I think the
> question for language extensions should never be "is there a good
> reason not to allow this", but "is it really necessary to allow this"?
> I.e., does it make things much easier to read, less error prone to
> write, and/or increase productivity a lot. I don't think the answer is
> "yes" for any of these questions as far as this extension is concerned.
Sorry I forgot to supply my real world example - I need to initialize
local string parameters to empty strings:
procedure Test;
var
A, B, C, D, E, F: string;
begin
A := '';
B := '';
C := '';
D := '';
E := '';
F := '';
// ....
end;
becomes:
procedure Test;
var
A, B, C, D, E, F: string = '';
begin
// ....
end;
Yes, both examples are much easier to read. You cannot convince me they
are not.
On 24.03.2018 16:53, Jonas Maebe wrote:
> When seeing that compound initialisation/declaration statement for
the first time, my first reflex was to interpret the initialisation as
only applying to the final variable, no matter how useless that would
make the language extension. As someone else posted, even languages that
do allow multiple variables to be assigned in a single statement use a
different syntax than the one to indicate that all variables have the
same type.
Could you please tell me what post do you refer to? I probably missed it.
If you are talking about Ozz Nixon's post and the C-syntax:
int a, b, c;
int a=0, b=0, c=0;
What different syntax do you talk about? The syntax is consequent - it's
about operator precedence. Comma is evaluated after the assignment (
http://en.cppreference.com/w/cpp/language/operator_precedence ) - this
is well documented. A similar scenario for Pascal would be: var A = 0, B
= 0, C = 0: Integer; But you know this is not Pascal syntax.
int a=b=0; is not supported in gcc
JavaScript: I am not aware you can declare type of variable in
JavaScript. The operator precedence is the same as in C:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
so var a=5,b=1; is logical and well documented like in C.
Yes, the alternative syntax var a=b=1; is valid in JavaScript.
So in Pascal, var A, B, C: Integer = 0; makes perfect sense and
undoubtedly means all three variables are initialized to 0
Ondrej
More information about the fpc-devel
mailing list