Lazarus

Free Pascal => FPC development => Topic started by: Ñuño_Martínez on September 19, 2016, 12:14:06 pm

Title: Al Gonzalez's "WITH" proposal
Post by: Ñuño_Martínez on September 19, 2016, 12:14:06 pm
Mexican Delphi specialist Al Gonzalez has made a proposal to upgrade the WITH keyword (https://twitter.com/algonzalez74/status/777158345710985216) that I think it's worth to be discussed.  We are discussing it at the Club Delphi forums (http://www.clubdelphi.com/foros/showthread.php?t=90849).

As you know, WITH keyword seems to be doomed as it's tagged as dangerous.  The proposal is something like this:
Code: Pascal  [Select][+][-]
  1.   WITH TForm2.Create (NIL) DO
  2.     TRY
  3.       AppLogger.LogForm (WITH);
  4.       WITH.ShowModal
  5.     FINALLY
  6.       WITH.Free
  7.     END
  8.  
Original post. (https://pbs.twimg.com/media/CskEHS6UEAAWMjp.jpg)

Somebody noted something similar in Python (http://www.clubdelphi.com/foros/showpost.php?p=508868&postcount=2):
Code: PHP  [Select][+][-]
  1. with open('file.txt', 'r') as f:
  2.        f.read()
  3.        d.write(...)
Other proposals (in the same thread at Club Delphi) are:
Code: Pascal  [Select][+][-]
  1.   WITH TForm2.Create (NIL) DO
  2.     TRY
  3.       &ShowModal
  4.     FINALLY
  5.       &Free
  6.     END
  7.  

and
Code: Pascal  [Select][+][-]
  1.   WITH TForm2.Create (NIL) DO
  2.     TRY
  3.       .ShowModal
  4.     FINALLY
  5.       .Free
  6.     END
  7.  

There's also a proposal to declare an alias (https://twitter.com/mdbs99/status/777337195929726976):
Code: Pascal  [Select][+][-]
  1.   WITH Obj: TForm2.Create (NIL) DO
  2.     TRY
  3.       Obj.ShowModal
  4.     FINALLY
  5.       Obj.Free
  6.     END
  7.  

Or use a default naming like SELF or RESULT.  The proposal is "IT" (https://twitter.com/algonzalez74/status/777692422658600960).

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][+][-]
  1. (* Initializes state. *)
  2.   PROCEDURE TGeckoMovement.Enter;
  3.   BEGIN
  4.     WITH TgoiGecko (StateMachine.Owner) DO
  5.     BEGIN
  6.       InicitAnimation (FP_WALK, FP_WALK + NUM_FP_CALK, V_ANIMATION);
  7.       Flipped := (Random (2) = 1)
  8.     END
  9.   END;
  10.  

But sometimes I use something like:
Code: Pascal  [Select][+][-]
  1. (* Initializes state. *)
  2.   PROCEDURE TGeckoMovement.Enter;
  3.   VAR
  4.     Gecko: TgoiGecko;
  5.   BEGIN
  6.     Gecko := TgoiGecko (StateMachine.Owner);
  7.     Gecko.InicitAnimation (FP_WALK, FP_WALK + NUM_FP_CALK, V_ANIMATION);
  8.     Gecko.Flipped := (Random (2) = 1)
  9.   END;
  10.  
I think that a redefinition would be useful.

What do you think?
Title: Re: Al Gonzalez's "WITH" proposal
Post by: marcov on September 19, 2016, 12:23:14 pm
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

Title: Re: Al Gonzalez's "WITH" proposal
Post by: rvk on September 19, 2016, 01:11:22 pm
As you know, WITH keyword seems to be doomed as it's tagged as dangerous.  The proposal is something like this:
Code: Pascal  [Select][+][-]
  1. with TForm2.Create(nil) do
  2. try
  3.   AppLogger.LogForm(with);
  4.   with.ShowModal
  5. finally
  6.   with.Free
  7. end;
What would be the advantage over this?
Code: Pascal  [Select][+][-]
  1. var
  2.   f: TForm2;
  3. begin
  4.   f := TForm2.Create(nil);
  5.   try
  6.     AppLogger.LogForm(f);
  7.     f.ShowModal
  8.   finally
  9.     f.Free
  10.   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][+][-]
  1. with TForm2.Create(nil) as myform do
  2. try
  3.   AppLogger.LogForm(myform);
  4.   myform.ShowModal
  5. finally
  6.   myform.Free
  7. end;
This would be similar as casting with "as" but with creating a variable on the fly.

(But again... that's not how Pascal works)
Title: Re: Al Gonzalez's "WITH" proposal
Post by: mse on September 19, 2016, 01:35:08 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)
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".

Title: Re: Al Gonzalez's "WITH" proposal
Post by: Michaela Joy on September 27, 2016, 12:39:05 am
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. :)
Title: Re: Al Gonzalez's "WITH" proposal
Post by: Remy Lebeau on September 29, 2016, 03:59:06 am
The only possible form I would see is with the "as"-keyword.
Code: Pascal  [Select][+][-]
  1. with TForm2.Create(nil) as myform do
  2. try
  3.   AppLogger.LogForm(myform);
  4.   myform.ShowModal
  5. finally
  6.   myform.Free
  7. end;

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.
Title: Re: Al Gonzalez's "WITH" proposal
Post by: skalogryz on September 29, 2016, 04:16:39 am
I 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)
TinyPortal © 2005-2018