Tommi Prami 130 Posted April 1, 2021 Sometimes removing withs, especially nested ones, are really tricky. Easy to mess up. Just got bit by this other day. IDE/Compiler/LSP must know which is which, so it would get first pass way faster than doing it manually. https://quality.embarcadero.com/browse/RSP-18953?filter=-2 On a side note thee has not been too much love towards refactoring features in IDE. Hope it would get more and ones we have to get better. -Tee- 2 Share this post Link to post
ConstantGardener 31 Posted April 1, 2021 Please fix the lsp in the first place and then, and only then, think about new stuff! Share this post Link to post
Guest Posted April 1, 2021 really is not necessary remove it... JUST DONT USE IT! 😂 hug Share this post Link to post
dummzeuch 1505 Posted April 1, 2021 1 hour ago, emailx45 said: really is not necessary remove it... JUST DONT USE IT! 😂 We have lots of legacy code that still uses "with" and I'm hesitant to touch this code because of this (Guess what's also missing? Unit tests. I add them when I find the time, but progress is slow.) A tool that reliably removes "with" statements would be awesome. Unfortunately it's also far from trivial. 1 Share this post Link to post
Vandrovnik 214 Posted April 1, 2021 Does Refactor / Rename work for you in 10.4.2? Here it always says something like this: Share this post Link to post
Tommi Prami 130 Posted April 16, 2021 On 4/1/2021 at 6:22 PM, dummzeuch said: We have lots of legacy code that still uses "with" and I'm hesitant to touch this code because of this (Guess what's also missing? Unit tests. I add them when I find the time, but progress is slow.) A tool that reliably removes "with" statements would be awesome. Unfortunately it's also far from trivial. Exactly. And also removing withs are quite pain by hand. More complicated it is. Nested with clauses are pure evil. Even ones that seems trivial are pain to remove sometimes. -Tee- Share this post Link to post
Fr0sT.Brutal 900 Posted April 16, 2021 IMHO Borland should've borrowed alias construction from SQL (select Foo as f, Bar as b ...) like this: with some.long.long.nested.object as foo do foo.bar := laz 1 Share this post Link to post
Uwe Raabe 2057 Posted April 16, 2021 55 minutes ago, Fr0sT.Brutal said: IMHO Borland should've borrowed alias construction from SQL (select Foo as f, Bar as b ...) like this: No need for that. You can already write this instead of such a with-clause. begin var foo := some.long.long.nested.object; foo.bar := laz ... end; That doesn't help converting existing with constructs, though. 1 Share this post Link to post
0x8000FFFF 22 Posted April 16, 2021 Even if there would be an option to define an "alias", some programmers will never give up using with clause. The would do it because they can. This is excerpt from InputQuery in Vcl.Dialogs.pas: Form := TInputQueryForm.CreateNew(Application); with Form do try ... finally Form.Free; end; Regarding the refactoring tool it would be probably useless (buggy as hell) considering that they still didn't get Code Insight/LSP right. Share this post Link to post
Anders Melander 1782 Posted April 16, 2021 55 minutes ago, Uwe Raabe said: No need for that. You can already write this instead of such a with-clause. begin var foo := some.long.long.nested.object; foo.bar := laz ... end; That doesn't help converting existing with constructs, though. Unfortunately inline vars are not always equivalent to "with". I'm currently working on a project that has, um.. let's be polite and say, "liberal" use of record arrays. So for example: type TFoo = record // Lots of stuff here end; TBar = record Foo: array of TFoo; // Even more stuff end; TFooBar = array of TBar; var FooBar: TFooBar; begin with FooBar[i].Foo[j] do WhatEver := 42; var Foo := FooBar[i].Foo[j]; Foo.WhatEver := 42; // Nope. end; Using an inline var to access an inner record will create a copy of the record while using "with" will use a reference to the record. Only way around that is to use record pointers but the code is horrible enough as it is. 2 Share this post Link to post
Fr0sT.Brutal 900 Posted April 16, 2021 3 hours ago, Uwe Raabe said: No need for that. You can already write this instead of such a with-clause. This feature took them 20 years and it still has drawbacks. If they've implemented aliases from the beginning, no troubles with with would happen ever Share this post Link to post
dummzeuch 1505 Posted April 16, 2021 (edited) 2 hours ago, Anders Melander said: Using an inline var to access an inner record will create a copy of the record while using "with" will use a reference to the record. Only way around that is to use record pointers but the code is horrible enough as it is. On the positive side: This way Delphi developers learn about pointers and how to use them correctly. 😉 type PFoo = ^TFoo; TFoo = record // Lots of stuff here end; PBar = ^TBar; TBar = record Foo: array of TFoo; // Even more stuff end; TFooBar = array of TBar; var FooBar: TFooBar; begin with FooBar[i].Foo[j] do WhatEver := 42; var Foo := @(FooBar[i].Foo[j]); Foo.WhatEver := 42; // Nope. end; (Now, It would be really embarrassing if I got this wrong. 😉 ) I'm always unsure whether I need the parenthesis in this: var Foo := @(FooBar[i].Foo[j]); Edited April 16, 2021 by dummzeuch Share this post Link to post
Stefan Glienke 2002 Posted April 16, 2021 4 minutes ago, dummzeuch said: (Now, It would be really embarrassing if I got this wrong. 😉 ) You got it almost - when using type inference on inline variables from pointers are no typed pointers but untyped so you have to explicitly state the type PFoo for your Foo variable. Share this post Link to post
dummzeuch 1505 Posted April 16, 2021 34 minutes ago, Stefan Glienke said: You got it almost - when using type inference on inline variables from pointers are no typed pointers but untyped so you have to explicitly state the type PFoo for your Foo variable. Ouch. I guess this shows that I'm not used to inline variables. Share this post Link to post
Uwe Raabe 2057 Posted April 16, 2021 58 minutes ago, Fr0sT.Brutal said: If they've implemented aliases from the beginning, no troubles with with would happen ever As long as no time machine is available it doesn't help much to lament about facts that cannot be changed afterwards. (One could as well claim that there would be no with-troubles when N.Wirth didn't invent it in the first place.) The topic is not about with being bad or how it could be made better, but about a tool to convert with statements without breaking existing code (which can be quite tricky shown by Anders). Share this post Link to post
dummzeuch 1505 Posted April 16, 2021 3 hours ago, Uwe Raabe said: As long as no time machine is available it doesn't help much to lament about facts that cannot be changed afterwards. (One could as well claim that there would be no with-troubles when N.Wirth didn't invent it in the first place.) The topic is not about with being bad or how it could be made better, but about a tool to convert with statements without breaking existing code (which can be quite tricky shown by Anders). So the solution is simple: Let's invent a time machine, go back to 1968 and kill N. Wirth. Problem solved. Or, a bit less blood thirsty: Get him involved with a girl that keeps him from inventing Pascal. 😉 Share this post Link to post