[Lazarus] 'with' is evil, isn't it?

Max Vlasov max.vlasov at gmail.com
Wed Aug 3 06:50:13 CEST 2011


On Tue, Aug 2, 2011 at 7:03 PM, Marcos Douglas <md at delfire.net> wrote:

> 2011/8/2 Max Vlasov <max.vlasov at gmail.com>:
> > Hi,
> >
> > Personally I stopped using 'with' keyword long time ago, but there are
> code
> > fragments from the past and today I fixed a little bug showing again that
> > it's a evil.
> > .....
> >
> > So not only the 'with' context can silently change while the project is
> > evolving, it can also change while it's being ported...
>
>

> IMHO, 'with' should have an implicit pointer like 'self' (could be
> 'this' or whatever).
>
> Eg:
>
> procedure TControlDescendant.Paint;
> begin
>  with Canvas do
>  begin
>       this.Rectangle(0, 0, this.Width, this.Height);
>  edn;
> end;
>
>
Reading all the discussion and suggestions, some thoughts came
1. Introducing a check in the compiler setting (or a conditional define)
that could force the developer to use self inside 'with' constructions. So
with this setting on, any access to fields, properties or methods will
require explicit self or 'identifier not found' will be fired.  This
solution still won't save from many problem situation (including the one I
gave an example), but at least it is compatible with existing Delphi syntax
and make some constructions more readable

2. Some switch (maybe on by default) that prevents from compiling if there
are several duplicate names in the same 'with' context (including global,
local, fields). The developer in this case has at least options to rename
local variables, include self for fields to resolve the problem or not using
with if there are no other options.
In contrary to 1. this would solve the example I gave since there are two
widths and heights in the context of 'with' construction.

What do you think?

Max
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20110803/7fb253a6/attachment-0003.html>


More information about the Lazarus mailing list