Is there a reason why multiple variable initialization is forbidden?
program Test;
var
A: Integer = 0; // allowed
B, C: Integer = 0; // not allowed
begin
end.Will a patch be applied that allows it?
Ondrej

Mailing lists / fpc-devel / 2018-03
27 messages · 14 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Is there a reason why multiple variable initialization is forbidden?
program Test;
var
A: Integer = 0; // allowed
B, C: Integer = 0; // not allowed
begin
end.Will a patch be applied that allows it?
Ondrej
If you were going to patch I'd also consider allowing:
program Test;
var
B, C: Integer = 0, 1; // sets B = 0, and C = 1
begin
end.Anthony Walter schrieb am Sa., 24. März 2018, 14:51:
No, that is only confusing.
Regards,
Sven
Is there a reason why multiple variable initialization is forbidden? program Test; var …
I think this is confusing to read.
Are B and C both initialized, or only B ?
Javascript also does not allow this, I think, and for once with good reason.
Michael.
The "integer" is applied to both. It would be inconsistent to apply the =0 only to C.
Javascript also does not allow this, I think, and for once with good reason.
Since when is JS a role model for Pascal?
Mattias
I think this is confusing to read. Are B and C both initialized, or only B ? Javascript…
It's not Mattias said, Integer is both applied to B and C, so should be "= 0".
Javascript also does not allow this, I think, and for once with good reason.
Well I cannot think of a good reason to disallow it :) And nobody has given a good reason yet.
Ondrej
On Sat, 24 Mar 2018 15:41:15 +0100 (CET) The "integer" is applied to both. It would be…
Actually (for clarity) JavaScript does allow multi-initialization, however, does not require type:
var a=0,b=0,c=0; // all are zero
// and if your presetting to zero due to Pascal not pre-initializing
variables, javascript does, so:
var a,b,c; // all are zero
var a=b=c=1; // C and most JavaScript engines all three are 1
var a,b,c=2; // in certain JavaScript engines only initializes C ==2, a and
b are zero.
some Pascal Dialets do support
var a:=b:=c:=3; // all are 3Ozz
Alexander Grotewohl wroteThe patch is to put them on separate lines. Preferably with meaningful comments for each. If FPC was changed at the whim of every programmer we'd end up with a huge mess that no longer resembles pascal.
Please tell me who decides about the borderline between whim and usefulness.
E.g. FPC supports initializing local variables, which Delphi and (if I am not mistaken) ISO Pascal don't support:
procedure Test;
var
A: Integer = 0;
begin
end;Is it a whim as well? If FPC has this extension, why not to support
procedure Test;
var
A, B: Integer = 0;
begin
end;?
Ondrej
It sounds more like you're making a case for all integers to be initialized to zero by default. I still fail to see any practical uses other than wanting to not type "integer" twice.
Alex
Is there a reason why multiple variable initialization is forbidden? program Test; var …
As the first one is supported, I see no logical reason the second shouldn't be supported, of course assigning 0 to B and C.
Please tell me who decides about the borderline between whim and usefulness. E.g. FPC…
It's in the eye of the beholder. Personally, I don't think it's a good extension either. Variable lists followed by a type are a fundamental part of the language and appear everywhere (variable lists, field declarations, parameter lists). On the other hand, a single constant that initialises multiple variables in a single statement does not exist anywhere else in the language. Therefore, adding it in one place would definitely be the wrong way to go about it. You'd also have to support it at least for record field initialisers and default parameters.
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. Having to switch your frame of mind when reading one particular family of expressions is bad for productivity. Everything should be as consistent as possible.
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.
Jonas
For the record the Oberon languages were designed with this philosophy in mind, and language changes only ever removed functionality (which did not happen often). Those languages never saw wide adoption but are very interesting specimens of engineering.
I might agree with an addition similar to Python's tuple unpacking in assignment, which would allow multiple assignment but require multiple values:
var
A, B: Integer = 0, 1;
C, D: Integer = 0, 0;However in some languages this is without good precedent or justification within the language, and seems like an unnatural bolted-on feature. Far more important I think is partial initialization of arrays and records.
Cheers,
R0b0t1
It's in the eye of the beholder. Personally, I don't think it's a good extension either.…
Jonas Maebe wroteYou'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);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.
Jonas Maebe wroteWhen 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
As the first one is supported, I see no logical reason the second shouldn't be supported,…
Good to know there's somebody with the same point-of-view. Thanks :)
Ondrej
and default parameters. Oh yes, I'd also love to write procedure Test(const A, B, C, D:…
Ondrej Pokorny wroteYes, both examples are much easier to read. You cannot convince me they are not.
Then there's not much point in discussing.
If you are talking about Ozz Nixon's post and the C-syntax:
I was.
The C-syntax is indeed consequent, and that is what I referred to: the assignments work the same way as elsewhere in the program, so multiple initialisations are just as clear as single initialisations there. In Pascal, you have a special syntax for initialised variables/constants, and hence extending it to work on multiple variables/fields does not have the advantage that you subconsciously immediately know what happens based on your knowledge of the rest of the language.
Jonas
Anthony Walter schrieb am Sa., 24. März 2018, 14:51: No, that is only confusing. Sven
+1
Writing programs in Pascal should make them readable, not add more obfuscation methods.
Ralf
---
This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus
Good to know there's somebody with the same point-of-view. Thanks :) Ondrej
As an extension, it makes sense - but I would make it a forced command line switch and obscure $modeswitch. So the code can easily be ported to other pascal compilers, or versions of FPC down the road. I spend 8 to 12 hours a day porting Delphi (versions) of code to FPC. And the XE series really made a mess of the language. I spend hours in FPC's core code, as I am making my script engine compatible with FPC's code (to be the interpreter for FPC source)...
I agree, especially from a script world, I would expect to see:
var a,b,c,d,e:String='';
as it looks like a script engines extension to:
const a,b,c,d:String='';// and in Delphi $J+ aka {$WRITEABLECONST ON} making constant read/write - you have basically produced what you are asking for. So, maybe FPC already supports this?
* Now side note, you can define variable types in JavaScript (wrote my own
ECMA compliant script engine in 2000 (DXJavaScript) for Delphi):
var x = new Number(0);
var y = new String("Hello World!");Of course, x and y are JavaScript objects at their core - so they can be any type later in the code. But, JavaScript does support type definition in the code. Number==Float==Integer.
Ozz
Ozz Nixon wroteAs an extension, it makes sense - but I would make it a forced command line switch and obscure $modeswitch.
Modeswitched and command-line options only go so far.
The problem with this approach is that, if I see someone elses code, I still have the confusing code in front of my eyes.
Same with Generics and whatnot.
It's correct that I don't use have to use them, but I am confronted with the ugly-as-hell syntax in other people's code all the time.
No amount of modeswitches can stop this proliferation of ugly constructs...
Michael.
Then there's not much point in discussing. I was. The C-syntax is indeed consequent, and…
I still don't understand how anybody can think that the syntax "var A, B: Integer = 0;" is unclear and doesn't immediately recognize that 0 is applied both to A and B. Honestly it would never come to my mind if you haven't raised this question.
As you Jonas said, Pascal syntax for initialization of variables/constants is a special one after all and therefore you do need to know it. Explaining/documenting the fact that you can initialize multiple variables/constants at once is one extra sentence. Just one sentence. In a documentation that you need to read anyway because the initialization syntax is unusual.
Anyway, my favorite example of non-pascalish syntax within FPC are the +=, -= etc. operators. I really don't understand how they made it into FPC if so many FPC developers are stubbornly against simple and helpful extensions :)
Ondrej
The were added in ~1996 when I maintained FPC by applying patches to a source tree on my hd :) But to be honest, I am not sure if I accepted the patch today :)
It sounds more like you're making a case for all integers to be initialized to zero by…
Pre pre-initialization, I do it as a compile option:
Modern Pascal v2.0.0 (LINUX,CPU64,X86_64,LITTLEENDIAN,1180323) Copyright (c) 2000..2017 by Ozz Nixon (Modern Pascal Solutions, LLC).
■ Produces Cross-Platform 32bit/64bit Encrypted Binaries.
/usr/bin/mp2 [options] sourcefile [modifiers]
options:
-2 286 instructions -3 386 instructions -6 64bit instructions -o objfpc compatible -g compile to binary -t syntax check only -p[0|1|2] Pack EXE -r<path> Resource dirs -v version display -q syntax & method check -? this screen -h history log
modifiers: (all are on by default)
-a- $A- Assert Support Off -m- $M- Auto-Initialization Off -b- $B- Boolean Short Circuit Off -o- $O- Overflow Checking Off -c- $C- New C Operators Off -p- $P- Auto-Properties Off -d- $D- Full Disposal Off -r- $R- Range Protection Off -f- $F- Autoinvoke Functions Off -s- $S- Enums Scoped Off -h- $H- Hint of unused variables Off -x- $X- Extended Syntax Off -j- $J- Constant Address Off -z- $Z- Case supports CONTINUE Off
-l- $L- Lazy Semicolon Off
All numeric values are 0, 0.0, Booleans 0 aka False, Strings = '' (blank), PChar and Pointers = Nil. * Now my product is to accent FPC not replace it in anyway. I use a separate code base, and use FPC to compile my compiler. My binaries are like .java or .Net managed binaries. I only have just over 1,000 end-users and we try to stick 100% compatible to Turbo Pascal 7, but support Delphi-isms and FPC-isms. It only takes a couple extra lines in asm to $M+ in my compiler (It has saved many customers who just assumed, I said "var a:longint" then a:=a+some_value (actually true PascalDevs never do that, it is my migrated clients who seem to insist on a+=some_value and then wonder why a!=some_value). So, I made MPC default to $M+.
For FPC, I would default to true Pascal, A is an address, you get whats there unless you initialize it, and most code I see, except for loops, A is never being assigned 0 its is always being assigned a value from input_source. So save the few uSec and don't pre-init.
My 2 cents from the script world...
O.
Is there a reason why multiple variable initialization is forbidden? program Test; var …
Ondrej Pokorny:
That means another complication of the language, to which it is not well suited, c.f. the C style:
int A = 0, B = 1, C = 2;Initialisation is a special and redundant way to as- sign a value to a variable. Is this mixing of dec- laration and action a good idea? Why not keep them separate?
If you must save vertical space, write your initial assignments on one line:
var A, B, C: Integer;
begin
A := 1; B := 2; C := 3;
{ work }
end;The were added in ~1996 when I maintained FPC by applying patches to a source tree on my…
Florian Klaempfl to Ondrej Pokorny:
On the other hand, '+=', '-=', '*=', and '/=' fit
perfectly with the genuine Wirthian operator ':='.Please tell me who decides about the borderline between whim and usefulness. E.g. FPC…
In our previous episode, Ondrej Pokorny said:
Yes.
Is there a reason why multiple variable initialization is forbidden? program Test; var …
Hi!
I have bean read all of the emails in this topic and now I have to write :) :
If multiple type declaration (A, B: Integer) are allowed then this new feature would not make difficulty to learn. Especially if we compare it to generics, what is already in the language and required/requires to learn a new (and non-conventional) syntax.
If the question is (as Jonas said) that "Is it really necessary to allow this"? - NOT necessary, we can live without it BUT...
BUT if ask the sub-questions (by Jonas):
- Does it make things much easier to read? - YES
- Less error prone to write? - YES
- Increase productivity a lot? - YES
Sum of answers are absolutely YES!
The requested language feature is a good idea that can make our favorite language better in all manners.
You can see it if you (all) take a look at at the examples (from Ondrej) below:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - -
OLD:
procedure Test(const A: Integer = 0; const B: Integer = 0; const C:
Integer = 0; const D: Integer = 0);NEW:
procedure Test(const A, B, C, D: Integer = 0);- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - -
OLD:
procedure Test;
var
A, B, C, D, E, F: string;
begin
A := '';
B := '';
C := '';
D := '';
E := '';
F := '';
end;NEW:
procedure Test;
var
A, B, C, D, E, F: string = '';
begin
end;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - -
OLD-MIXED:
procedure Test(const A: Integer = 0; const B: Integer = 0; const C:
Integer = 0; const D: Integer = 0);
var
E, F, G, H,,I, J: string;
begin
E := '';
F := '';
G := '';
H := '';
I := '';
J := '';
end;NEW-MIXED:
procedure Test(const A, B, C, D: Integer = 0);
var
E, F, G, H, I, J: string = '';
begin
end;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - -
And again, the requested language feature is a good idea that can make our favorite language better in all manners.
2018-03-24 11:46 keltezéssel, Ondrej Pokorny írta:
Regards
uses SysUtils; // inttostr
procedure blank(a: array of pointer);
var
i: longint;
begin
for i:=low(a) to high(a) do
// string(a[i]^):='';
string(a[i]^):='silly ~~~~~~~ '+IntToStr(i);
end;
var
s, i, l, {l,} y: string;
begin
blank(, @i, @l, ,} @y]);
writeln(s); writeln(i);
writeln(l); writeln(l);
writeln(y); // !!
end.Ondrej Pokorny: That means another complication of the language, to which it is not well…
Anton Shepelev wroteInitialisation is a special and redundant way to assign a value to a variable. Is this mixing of declaration and action a good idea? Why not keep them separate?
LOL, I am not the one who wants to introduce initialisation of variables to Pascal. For your information, implicit and explicit initialization of variables and fields are in Pascal already now.
Ondrej