* * *

Author Topic: Al Gonzalez's "WITH" proposal  (Read 2445 times)

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 654
    • Burdjia
Al Gonzalez's "WITH" proposal
« on: September 19, 2016, 12:14:06 pm »
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]
  1.   WITH TForm2.Create (NIL) DO
  2.     TRY
  3.       AppLogger.LogForm (WITH);
  4.       WITH.ShowModal
  5.     FINALLY
  6.       WITH.Free
  7.     END
  8.  
  • 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]
  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:
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".

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?
« Last Edit: September 19, 2016, 12:27:00 pm by Ñuño_Martínez »

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 5826
Re: Al Gonzalez's "WITH" proposal
« Reply #1 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

« Last Edit: September 27, 2016, 11:03:55 am by marcov »

rvk

  • Hero Member
  • *****
  • Posts: 2945
Re: Al Gonzalez's "WITH" proposal
« Reply #2 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)

mse

  • Full Member
  • ***
  • Posts: 234
Re: Al Gonzalez's "WITH" proposal
« Reply #3 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".


Michaela Joy

  • New member
  • *
  • Posts: 24
Re: Al Gonzalez's "WITH" proposal
« Reply #4 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. :)

Remy Lebeau

  • Sr. Member
  • ****
  • Posts: 335
    • Lebeau Software
Re: Al Gonzalez's "WITH" proposal
« Reply #5 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.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) open source project - Admin, Developer

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1826
Re: Al Gonzalez's "WITH" proposal
« Reply #6 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)
« Last Edit: September 29, 2016, 04:29:15 am by skalogryz »

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus