Oh boy, VB.NET and JavaScript both have a `with` keyword too!
Posted by jpluimers on 2024/03/05
Last year, within a week, I saw two tweets of languages that, like Pascal, have a with
statement as well:
- [Archive.is] Shawn Wildermuth 💻☕🎸🎥🎮 on Twitter: “JavaScript’s Forgotten Keyword (with)”
- [Archive.is] John Kaster #BlackLivesMatter on Twitter: “@suited_aces @marcocantu @delphijunkie @JimMcKeeth @jpluimers I present “with”… “
The first points to an article that shows the JavaScript implementation of with
is very similar to the Pascal one: [Wayback] JavaScript’s Forgotten Keyword (with) – DEV Community.
Just in case some of my readers do not know my opinion of the Pascal with
statement (it even has it’s own blog category), I really think you should not use it Delphi: you should avoid the with
statement as it makes your code less future proof.
The reason not to use it is called [Wayback] Accidental Shadowing in computer language speak (it also can rear its head when you define variables at different block levels like for instance this golang example: [Wayback] Warning for accidental variable shadowing with block scope – Technical Discussion – Go Forum).
Even the JavaScript specification advises against using the with
keyword in [Wayback] with – JavaScript | MDN
**Warning:**Use of the with statement is not recommended, as it may be the source of confusing bugs and compatibility issues. See the “Ambiguity Contra” paragraph in the “Description” section below for details.
There have been various proposals to extend the Delphi implementation of the with
statement to make it more resilient to Accidental Shadowing by forcing the usage to be prepended by a .
(dot) or alias, as for instance seen in [Wayback] Re: “with” Coders are Monsters – delphi / [Wayback] delphi • View topic • “with” Coders are Monsters:
- Wayback – Report No: 679 Status: Open
The infamous WITH resolution
qc.codegear.com/wc/qcmain.aspx
QCWIN:Defect_No=679 - Wayback – Report No: 803 Status: Reported
Proposal for another extension to ‘with’ statement
qc.codegear.com/wc/qcmain.aspx
QCWIN:Defect_No=803 - Wayback – Report No: 6867 Status: Open
Extend ‘with’ statement to allow ‘with <alias>:= <expression>do’
qc.codegear.com/wc/qcmain.aspx
QCWIN:Defect_No=6867 - Wayback – Report No: 38563 Status: Open (dup of 679)
with statement really should support aliases
qc.codegear.com/wc/qcmain.aspx
QCWIN:Defect_No=38563
This is in fact what the second twitter messages pointed to: a VB.NET example doing just that: prepend with a dot: [Wayback] Maarten Balliauw on Twitter: “Looks like using With
makes it pretty clean!… “
I was not even aware that VB.NET had it, but it has: [Wayback] With…End With Statement – Visual Basic | Microsoft Docs
And it has similar debugging issues as with Delphi as per [Wayback] The VB.NET ‘With’ Statement – embrace or avoid? – Stack Overflow:
Find the beginning of a With statement and set a breakpoint. Step to the next line (so you’re hiding the first line right under the if block). Highlight it, then ‘Add Watch’. You should see this: ‘With’ contexts and statements are not valid in debug windows.
–jeroen
Leave a comment