Al Gonzalez's "WITH" proposal lazarus / 64-FPC development ---------------------------------------------------------------------- Ñuño_Martínez · 2016-09-19 10:14 Mexican Delphi specialist Al Gonzalez has made a proposal to upgrade the WITH keyword that I think it's worth to be discussed.  We are discussing it at the Club Delphi forums. As you know, WITH keyword seems to be doomed as it's tagged as dangerous.  The proposal is something like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   WITH TForm2.Create (NIL) DO    TRY      AppLogger.LogForm (WITH);      WITH.ShowModal    FINALLY      WITH.Free    END No more than one object/record per WITH block.No nested WITH blocks allowed.Mandatory WITH keyword or other symbol to indicate the referenced object.Original post. Somebody noted something similar in Python: Code: PHP  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i with open('file.txt', 'r') as f:       f.read()       d.write(...)Other proposals (in the same thread at Club Delphi) are: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   WITH TForm2.Create (NIL) DO    TRY      &ShowModal    FINALLY      &Free  END  and Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   WITH TForm2.Create (NIL) DO    TRY      .ShowModal    FINALLY      .Free  END  There's also a proposal to declare an alias: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   WITH Obj: TForm2.Create (NIL) DO    TRY      Obj.ShowModal    FINALLY      Obj.Free    END  Or use a default naming like SELF or RESULT.  The proposal is "IT". My opinion is mixed.  I don't like WITH.  Years ago I used it a lot in a big project and now it's hard to read that code, so now I used it a few times, mostly in small code snipets like: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i (* Initializes state. *)  PROCEDURE TGeckoMovement.Enter;  BEGIN    WITH TgoiGecko (StateMachine.Owner) DO    BEGIN      InicitAnimation (FP_WALK, FP_WALK + NUM_FP_CALK, V_ANIMATION);      Flipped := (Random (2) = 1)    END  END;  But sometimes I use something like: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i (* Initializes state. *)  PROCEDURE TGeckoMovement.Enter;  VAR    Gecko: TgoiGecko;  BEGIN    Gecko := TgoiGecko (StateMachine.Owner);    Gecko.InicitAnimation (FP_WALK, FP_WALK + NUM_FP_CALK, V_ANIMATION);  Gecko.Flipped := (Random (2) = 1)  END; I think that a redefinition would be useful. What do you think? ---------------------------------------------------------------------- marcov · 2016-09-19 10:23 I don't like it since it breaks the predeclaration of variables of Pascal. Maybe Python allows inline declaration of variables, but Pascal definitely not. I also think the problem is not big enough to actually warrant extra syntax.  IMHO it is only needlessly expanding the language as a hobby language designer. The big problems are not in language syntax, but in (multiplatform) libraries and maintenance ---------------------------------------------------------------------- rvk · 2016-09-19 11:11 Quote from: Ñuño_Martínez on September 19, 2016, 12:14:06 pmAs you know, WITH keyword seems to be doomed as it's tagged as dangerous.  The proposal is something like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i with TForm2.Create(nil) dotry  AppLogger.LogForm(with);  with.ShowModalfinally  with.Freeend;What would be the advantage over this? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  f: TForm2;begin  f := TForm2.Create(nil);  try    AppLogger.LogForm(f);  f.ShowModal  finally    f.Free  end;In both cases you can use short variable-names. Only in Pascal you would need to declare the variable (which, like marcov also mentioned, would always be needed). Even for the "for x in y", the x needs to be declared. The only possible form I would see is with the "as"-keyword. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i with TForm2.Create(nil) as myform dotry  AppLogger.LogForm(myform);  myform.ShowModalfinally  myform.Freeend;This would be similar as casting with "as" but with creating a variable on the fly. (But again... that's not how Pascal works) ---------------------------------------------------------------------- mse · 2016-09-19 11:35 Quote from: rvk on September 19, 2016, 01:11:22 pmThis would be similar as casting with "as" but with creating a variable on the fly. (But again... that's not how Pascal works) Actually Pascal "with" statement creates a pointer variable on the fly, what is missing that it gets a name. There is a very old Pascal dialect which supports such a safe with statement, I don't remember the name, you'll find it in FPC mailing list archive. It has been discussed many times. I long gave up to hope that Free Pascal will support it too, unless it becomes "Delphi compatible". ---------------------------------------------------------------------- Michaela Joy · 2016-09-26 22:39 Personally, I wouldn't use it. I'm not keen about using the with statement as it is. There are too many instances where it makes the code appear to be unclear. (i.e. properties / methods appear to clash) But that's me. Others may and probably will see it differently. @rvk: I agree with you. I see your example as being clearer and easier to read and understand. Just my opinion. FWIW. ---------------------------------------------------------------------- Remy Lebeau · 2016-09-29 01:59 Quote from: rvk on September 19, 2016, 01:11:22 pmThe only possible form I would see is with the "as"-keyword. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i with TForm2.Create(nil) as myform dotry  AppLogger.LogForm(myform);  myform.ShowModalfinally  myform.Freeend; Except the code would be ambiguous if a real type-cast is needed and the 'with' operates on the result of the cast.  If you are going to introduce aliasing, a new syntax would be needed that is not ambiguous. ---------------------------------------------------------------------- skalogryz · 2016-09-29 02:16 Quote from: marcov on September 19, 2016, 12:23:14 pmI don't like it since it breaks the predeclaration of variables of Pascal. the only exception is made for exceptions Code: [Select]try  .. except   on e: exception do ... end; (need to be noted, this is not quite an exception to predeclaration rule, since the exception variable is predeclared ahead of the handling code. But it definitely looks like inline declaration, similar to C-ish Code: [Select]for (var i=0...)  ) On topic: these days IDEs have all sorts of features for mass-code generation/editing, that it makes with-statement irrelevant (no matter if syntax is changed or not)