Variables everywhere - is it good idea? lazarus / 64-FPC development ---------------------------------------------------------------------- moskalenco_a · 2015-03-15 19:02 Hi all.What do you think about this idea?Example, Code: [Select]begin var a:Integer; a:=0; for var i:=1 to 10 do  begin   var c:=i*2;   a:=a+c;  end; Writeln(a); Readln; end.Is it good idea or is it bad? ---------------------------------------------------------------------- Never · 2015-03-15 19:10 these expresions are currently illegal the above code will not compile... if this is a suggestion imho : no thank you very much this a nice way to lose the eggs and the basket in 10 lines of code ---------------------------------------------------------------------- moskalenco_a · 2015-03-15 19:12 Yes,it's suggestion. ---------------------------------------------------------------------- Nitorami · 2015-03-15 19:37 This is useless. In a construct like for var i := 1 to 10 do what is "var" good for ? It has no meaning. In Basic, you can omit it competely for i = 1 to 10 do ... In C, you need to specify the type of variable for (int i=1; i <=10; i++) Pascal forces you to specify it beforehand, and that is intended. Code: [Select]var a,i,c:Integer; begin a:=0; for i:=1 to 10 do  begin   c:=i*2;   a:=a+c;  end; Writeln(a); Readln; end. ---------------------------------------------------------------------- marcov · 2015-03-15 21:15 It can be used to disambiguate that you really want to create a variable in that scope, overriding a possible local variable called I. That being said with most current IDE having shortcuts to properly declare variables, I think it is an outdated concept. It was probably important to micromanage stack depth in the C of 1970, but nowadays barely anything is dynamically allocated from the stack anymore (alloca in C), and most big types are not allocated from the stack. ---------------------------------------------------------------------- Nitorami · 2015-03-15 21:52 Interesting. But within a function or procedure, any var declaration is allocated on the stack, isn't it ? What is the difference to alloca then ? ---------------------------------------------------------------------- Leledumbo · 2015-03-16 03:40 Quote from: FPC on March 15, 2015, 08:02:45 pmHi all.What do you think about this idea?Example, Code: [Select]begin var a:Integer; a:=0; for var i:=1 to 10 do  begin   var c:=i*2;   a:=a+c;  end; Writeln(a); Readln; end.Is it good idea or is it bad? Bad, but if you want, try GNU Pascal. They already have it implemented. I don't think FPC will ever implement it, though. ---------------------------------------------------------------------- marcov · 2015-03-16 04:27 Quote from: Nitorami on March 15, 2015, 10:52:07 pmInteresting. But within a function or procedure, any var declaration is allocated on the stack, isn't it ? What is the difference to alloca then ? There is no need to actually also implement it in a nested way. (since that would only generate more instructions). And afaik alloca can take a variable count. ---------------------------------------------------------------------- alcalde · 2015-08-09 20:02 Quote from: FPC on March 15, 2015, 08:02:45 pmHi all.What do you think about this idea?Example, Code: [Select]begin var a:Integer; a:=0; for var i:=1 to 10 do  begin   var c:=i*2;   a:=a+c;  end; Writeln(a); Readln; end.Is it good idea or is it bad? If anything it's still too verbose.  There was no need to even declare the "a" variable first since it was assigned the value 0. But otherwise, yes, it's a good first step towards bringing Pascal up to date with modern "best practices" in language design.. I can dig up a computer science paper in which it states that "it's now universally accepted that variables should be declared as close as possible to where they were first used". Pascal's declare everything ahead syntax was motivated by Wirth's single-pass compiler design, which was itself motivated by extreme memory limitations at the time. As he put it, when he designed Pascal, "the memory of mainframes was measured in kilobytes".  Over the years Pascal users have invented all sorts of reasons why things were designed the way they were, but the reality is generally that the oddities of Pascal were all the result of the single pass compiler, not because they conferred some sort of benefit to coding. ---------------------------------------------------------------------- alcalde · 2015-08-09 20:17 Quote from: Leledumbo on March 16, 2015, 04:40:46 amBad, but if you want, try GNU Pascal. They already have it implemented. I don't think FPC will ever implement it, though. Why do you believe it's bad? I've done a formal review of languages for two start-ups, and I don't recall seeing a single statically-typed language created in the 21st century (2000-) that didn't use type inference. In computer science it's considered universally true that a variable should be declared as close as possible to where it is first used (in Pascal it's the old page-up, page-up, page-up... sigh). Pascal's declare-everything-upfront design was necessitated by Wirth's use of a single-pass compiler design to save memory. There's simply no need in 2015 to clutter code by pedantically declaring every variable and the good old page-up, page-down (no, IDE improvements should not be used to cover up language shortcomings). If you think of languages as organisms that evolve and pass on their good ideas to future languages, then the notion of defining every variable in a pre-defined section was an evolutionary dead end. Other than COBOL and close Pascal cousins ADA and Eiffel, absolutely no language in decades adopted the notion. Certainly no popular language today uses it. If you were trying to convince a C++, Ruby, Java developer to use Pascal, how would you answer them when they asked why they had to declare all of their variables beforehand, especially when they point out that the Handley-Millner type inference algorithm has existed for 30 years? And if we can't answer, where does the next generation of Pascal developers come from? ---------------------------------------------------------------------- taazz · 2015-08-09 20:43 Quote from: alcalde on August 09, 2015, 10:17:07 pmQuote from: Leledumbo on March 16, 2015, 04:40:46 amBad, but if you want, try GNU Pascal. They already have it implemented. I don't think FPC will ever implement it, though. Why do you believe it's bad? I've done a formal review of languages for two start-ups, and I don't recall seeing a single statically-typed language created in the 21st century (2000-) that didn't use type inference. In computer science it's considered universally true that a variable should be declared as close as possible to where it is first used (in Pascal it's the old page-up, page-up, page-up... sigh). Pascal's declare-everything-upfront design was necessitated by Wirth's use of a single-pass compiler design to save memory. There's simply no need in 2015 to clutter code by pedantically declaring every variable and the good old page-up, page-down (no, IDE improvements should not be used to cover up language shortcomings). If you think of languages as organisms that evolve and pass on their good ideas to future languages, then the notion of defining every variable in a pre-defined section was an evolutionary dead end. Other than COBOL and close Pascal cousins ADA and Eiffel, absolutely no language in decades adopted the notion. Certainly no popular language today uses it. If you were trying to convince a C++, Ruby, Java developer to use Pascal, how would you answer them when they asked why they had to declare all of their variables beforehand, especially when they point out that the Handley-Millner type inference algorithm has existed for 30 years? And if we can't answer, where does the next generation of Pascal developers come from? how about design first code later? If you already have a solid design then where the variables are declared should not be an issue if on the other hand you are hacking away trying to come to a solution from your keyboard then yes declaring when you need it or even never declaring it and letting the first assignment define its data type should help a lot, then again why would I want to use such code? ---------------------------------------------------------------------- Michl · 2015-08-09 21:40 If you use Lazarus and you don't want to step to the declaration, you can use the code completion. For example write: Quoteprocedure TForm1.Button1Click(Sender: TObject); begin   for i:=1 to 10 do end;  Step back to the "i" and press [CTRL] + [SHIFT] + [C] and see the magic ---------------------------------------------------------------------- Edson · 2015-08-10 02:18 Quote from: FPC on March 15, 2015, 08:02:45 pmHi all.What do you think about this idea?Example, Code: [Select]begin var a:Integer; a:=0; for var i:=1 to 10 do  begin   var c:=i*2;   a:=a+c;  end; Writeln(a); Readln; end.Is it good idea or is it bad? IMHO, it's a modern feature in an antique programming language. It has not the spirit of Pascal but could be useful, like some other modern additions. ---------------------------------------------------------------------- Basile B. · 2015-08-10 03:11 DWS has it. (https://www.delphitools.info/dwscript). Using var as keyword seems to be motivated to avoid the proliferation of keywords. Actually it wouldn't be a problem since inside a function body var is currently illegal. But if it has to be done, let's open the breach fully: not only for to, for downto, but also for in and every local declaration. ---------------------------------------------------------------------- Leledumbo · 2015-08-10 05:18 Quote from: alcalde on August 09, 2015, 10:17:07 pmWhy do you believe it's bad? I've done a formal review of languages for two start-ups, and I don't recall seeing a single statically-typed language created in the 21st century (2000-) that didn't use type inference. In computer science it's considered universally true that a variable should be declared as close as possible to where it is first used (in Pascal it's the old page-up, page-up, page-up... sigh). Pascal's declare-everything-upfront design was necessitated by Wirth's use of a single-pass compiler design to save memory. There's simply no need in 2015 to clutter code by pedantically declaring every variable and the good old page-up, page-down (no, IDE improvements should not be used to cover up language shortcomings). I'm a supporter of such a construct for the sake of readability. It's not only for the compiler, but more for the human. It reduces reading complexity due to mixed declaration-execution, which quite the contrary with your saying, clutters the code more than separated declaration-execution. Page up-page down is better since it's consistent, rather than unpredictable up-down because the variable could be declared nearly everywhere. Quote from: alcalde on August 09, 2015, 10:17:07 pmIf you think of languages as organisms that evolve and pass on their good ideas to future languages, then the notion of defining every variable in a pre- defined section was an evolutionary dead end. Other than COBOL and close Pascal cousins ADA and Eiffel, absolutely no language in decades adopted the notion. Certainly no popular language today uses it. So what? I'm not making Pascal another language. This concept is good, no popular uses it is another thing. Who says popular = better? Quote from: alcalde on August 09, 2015, 10:17:07 pmIf you were trying to convince a C++, Ruby, Java developer to use Pascal, how would you answer them when they asked why they had to declare all of their variables beforehand, especially when they point out that the Handley- Millner type inference algorithm has existed for 30 years? And if we can't answer, where does the next generation of Pascal developers come from? I won't try to convince them to use their other-language-mind to code Pascal. If you want to code in Pascal, think in Pascal. I accept the lack of static checking and undetectable variable shadowing in PHP/Ruby/Lua/any language without variable declaration and dynamic typing, I accept the lack of simple procedural coding when coding short simple programs in Java, I accept the lack of modular programming when coding in C/C++, why shouldn't they accept Pascal concept if they have to code in Pascal? FYI, Handley-Millner type inference algorithm is not perfect for imperative languages. In certain simple cases, they might be applicable. But the lack of type inference again improves readability because readers quickly know what the type of a variable is. With type inference, only the compiler knows, at least without analyzing the code deep enough. At the very least one must read documentation or even actual source code. Code: [Select]x := StringsReplace(SomeText,['a','b'],['x','y'],[rfReplaceAll]); Without ever knowing what StringsReplace is, how do you know the type of x? How are you sure that it returns replaced copy of the string instead of returning the number of successful replacements with the string itself is modified inplace? ---------------------------------------------------------------------- marcov · 2015-08-10 07:55 Quote from: alcalde on August 09, 2015, 10:17:07 pmWhy do you believe it's bad? I've done a formal review of languages for two start-ups, and I don't recall seeing a single statically-typed language created in the 21st century (2000-) that didn't use type inference. In computer science it's considered universally true that a variable should be declared as close as possible to where it is first used (in Pascal it's the old page-up, page-up, page-up... sigh). In computer science it is also considered universally true that you should keep procedures and methods short, so there should be no need for page-up and page-down :-) ---------------------------------------------------------------------- dieselfan · 2015-08-13 11:01 Quote from: alcalde on August 09, 2015, 10:17:07 pmWhy do you believe it's bad? I've done a formal review of languages for two start-ups, and I don't recall seeing a single statically-typed language created in the 21st century (2000-) that didn't use type inference. In computer science it's considered universally true that a variable should be declared as close as possible to where it is first used (in Pascal it's the old page-up, page-up, page-up... sigh). Hence intialisation SHOULD occur on first few lines . That would comply with what you learnt? **** Var x: integer; begin   x := 0; **** Quote from: marcov on August 10, 2015, 09:55:13 amIn computer science it is also considered universally true that you should keep procedures and methods short, so there should be no need for page-up and page- down :-) Yes and in the OP's post there would be many "var" instances leading to longer code...vs just ONE above BEGIN. ---------------------------------------------------------------------- User137 · 2015-08-13 16:01 All i know is, if such feature came to be i definitely would use it. Alot. Recently i'm dealing with C# since it's for commercial games with Unity, but this language feature is convenient. Code: [Select]for var i: integer:=0 to 9 do... // Or for integer i:=1 to 10 do Just saying "for var i:=1 to 10 do" is impossibility, it has to be typed. "for (int i=1; i<11; i++)" from C world the "int" is a specific type, not just general loop counter, and could be anything else too. For who cares it could be "for (float i=1.5; i<12.33; i+=0.6)" ---------------------------------------------------------------------- Leledumbo · 2015-08-14 09:03 Quote from: User137 on August 13, 2015, 06:01:09 pm"for (int i=1; i<11; i++)" from C world the "int" is a specific type, not just general loop counter, and could be anything else too. For who cares it could be "for (float i=1.5; i<12.33; i+=0.6)" That's the difference with Pascal's for. In those languages, for loop is a syntactic sugar for while loop. In Pascal, for can do what while can't do, so does the other way around. ---------------------------------------------------------------------- Thaddy · 2015-08-14 09:51 Quote from: Leledumbo on August 14, 2015, 11:03:23 amThat's the difference with Pascal's for. In those languages, for loop is a syntactic sugar for while loop. In Pascal, for can do what while can't do, so does the other way around. You obviously didn't study any compiler history. Or processor history for that matter. Or algorithms in general. What you state is false. If a loop can be determinded to end in finite space ..... Boogle or Ging on that one. Sigh. Mayor lack. I object to syntactic sugar. It isn't in Pascal, but it also isn't in COBOL(1959) or BASIC(1964) which are much older then Pascal(1971). Regularly it is a processor feature/ assembler convention, like (x)cx on Intel. It's called a counter... ---------------------------------------------------------------------- Leledumbo · 2015-08-14 10:58 Quote from: Thaddy on August 14, 2015, 11:51:07 amYou obviously didn't study any compiler history. Or processor history for that matter. Or algorithms in general. What you state is false. Quite the opposite, not only compiler history, I also learned compiler construction and programming language design. For loop in C family IS syntactic sugar: Code: [Select]for (init;cond;inc)  { body; } is syntactically and semantically equivalent with: Code: [Select]init; while (cond) { body; inc; } Hence, you can't do: Code: [Select]unsigned char i; for (i = 0; i <= 255; i++) { ...; } above for loop goes infinitely or crash with overflow exception depending on the language. OTOH, while you can't do: Code: [Select]var b: Byte; b := 0; while b <= 255 do begin   ...   Inc(b); end; which will trigger range check error, you can do: Code: [Select]var b: Byte; for b := 0 to 255 do ...; which will perfectly runs the loop 256 times. Objection? Chapter 6.8.3.9 of the ISO standard shall enlighten you about for and while loop equivalence in Pascal, and that's no syntactic sugar. ---------------------------------------------------------------------- eny · 2015-08-14 14:55 Quote from: Thaddy on August 14, 2015, 11:51:07 amQuote from: Leledumbo on August 14, 2015, 11:03:23 amThat's the difference with Pascal's for. In those languages, for loop is a syntactic sugar for while loop. In Pascal, for can do what while can't do, so does the other way around. You obviously didn't study any compiler history. Or processor history for that matter. Or algorithms in general. What you state is false. ---------------------------------------------------------------------- skalogryz · 2015-08-14 17:04 Quote from: alcalde on August 09, 2015, 10:02:39 pmBut otherwise, yes, it's a good first step towards bringing Pascal up to date with modern "best practices" in language design.. I can dig up a computer science paper in which it states that "it's now universally accepted that variables should be declared as close as possible to where they were first used". Actually it's going the opposite now. Specifically, because of the popularity of using closures. Closure requires a declaration of the variable prior to the declaration of closure (anon. function) That brings a developer to declare all (for consistency) variables in one place at the beggining of the code. The only exception is loop variables. But due to informal convention of naming loop variables (i.e. "i", "j","k") it's normal to declare them ahead of the code. Here's the most recent example I've touched. Example of dataTable.js code (jquery.dataTables.js): Code: [Select] /** * Draw the table for the first time, adding all required features *  @param {object} settings dataTables settings object *  @memberof DataTable#oApi */ function _fnInitialise ( settings ) { var i, iLen, iAjaxStart=settings.iInitDisplayStart; var columns = settings.aoColumns, column; var features = settings.oFeatures; /* Ensure that the table data is fully initialised */ if ( ! settings.bInitialised ) { setTimeout( function(){ _fnInitialise( settings ); }, 200 ); return; } ---------------------------------------------------------------------- lazjump · 2015-08-14 17:24 I read this thread and I appreciate how Leledumbo explains things clearly ---------------------------------------------------------------------- skalogryz · 2015-08-14 17:41 Quote from: Leledumbo on August 14, 2015, 11:03:23 amQuote from: User137 on August 13, 2015, 06:01:09 pm"for (int i=1; i<11; i++)" from C world the "int" is a specific type, not just general loop counter, and could be anything else too. For who cares it could be "for (float i=1.5; i<12.33; i+=0.6)" That's the difference with Pascal's for. In those languages, for loop is a syntactic sugar for while loop. In Pascal, for can do what while can't do, so does the other way around. (Object) Pascal for-loop is not C for() in a few ways (where C for- loop is indeed a sugar over while): * the number of iteration is estimated once and prior to execution of the loop. That might take effect on the efficiency and logic of the code. for example Code: [Select]for i:=0 to list.Count-1 do   ... and Code: [Select]i:=0; while i