Forum > FPC development

Al Gonzalez's "WITH" proposal

(1/2) > >>

Ñuño_Martínez:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---with open('file.txt', 'r') as f:       f.read()       d.write(...)Other proposals (in the same thread at Club Delphi) are:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  WITH TForm2.Create (NIL) DO    TRY      &ShowModal    FINALLY      &Free    END 
and

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  WITH TForm2.Create (NIL) DO    TRY      .ShowModal    FINALLY      .Free    END 
There's also a proposal to declare an alias:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---(* 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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---(* 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:
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:

--- Quote from: Ñuño_Martínez on September 19, 2016, 12:14:06 pm ---As you know, WITH keyword seems to be doomed as it's tagged as dangerous.  The proposal is something like this:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---with TForm2.Create(nil) dotry  AppLogger.LogForm(with);  with.ShowModalfinally  with.Freeend;
--- End quote ---
What would be the advantage over this?

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:

--- Quote from: rvk on September 19, 2016, 01:11:22 pm ---This would be similar as casting with "as" but with creating a variable on the fly.

(But again... that's not how Pascal works)

--- End quote ---
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:
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. :)

Navigation

[0] Message Index

[#] Next page

Go to full version