Board index » delphi » (dis)advantages of 'with'?

(dis)advantages of 'with'?

I found 'with' not working with some array types (of records), and therefore
would like to know more about the 'with' statement.

In detail, a pointer to a specific data item (i.e. to someArray[i]) always
works as a shortcut to that item, but 'with' seems to work different from that
model.

In addition, an explicit pointer to any item will remove any ambiguosity, when
sub-items of the same name exist in different scopes at the same time.

In VB a leading period would unambiguously define, that the 'with' item is
referred to by the following method or property names. At least in single-level
'with' blocks. In Delphi instead explicit references to 'Self' or other scopes
are required, to distinguish between members of different scopes. This
requirement often makes the code even longer instead of shorter, when 'with' is
used.

Now I'm undecided whether and when it's practical, possible and safe to use
'with', instead of an explicit pointer or reference to some data item. Does
'with' only declare a prefix, that is evaluated again with every usage in the
'with' block, or does it really use some temporary direct reference to the
selected data item?

DoDi

 

Re:(dis)advantages of 'with'?


Quote
: vb...@aol.com (VBDis) wrote:

>I found 'with' not working with some array types (of records), and therefore
>would like to know more about the 'with' statement.

It always works.

Quote
>Now I'm undecided whether and when it's practical,

This is up to you.

Quote
>possible

Always.

Quote
>and safe to use

This again depends upon your style.

I personally make a point NOT to extend a with over more than two or
three lines of code and especially of NOT nesting with statements.

"with" used to be useful for optimization. Now it is mostly syntactic
sugar.

--
Stefan Hoffmeister    (http://www.econos.de/)
No private email, please, unless expressly invited.

Re:(dis)advantages of 'with'?


On 05 Feb 1999 07:44:09 GMT, vb...@aol.com (VBDis) wrote:

Quote
>I found 'with' not working with some array types (of records), and therefore
>would like to know more about the 'with' statement.

WITH works, but it sometimes surprises unsuspecting programmers.

Quote
>In addition, an explicit pointer to any item will remove any ambiguosity, when
>sub-items of the same name exist in different scopes at the same time.

True, and for that reason, I strongly discourage using WITH.

Quote
>Now I'm undecided whether and when it's practical, possible and safe to use
>'with', instead of an explicit pointer or reference to some data item. Does
>'with' only declare a prefix, that is evaluated again with every usage in the
>'with' block, or does it really use some temporary direct reference to the
>selected data item?

WITH uses a hidden temporary variable. Thus,

with Object.SomeHideouslyExpensiveProperty do
begin
  A := X;
  B := Y;
end;

is more "efficient" than

Object.SomeHideouslyExpensiveProperty.A := X;
Object.SomeHideouslyExpensiveProperty.B := Y;

But

var
  SaveTime: THideouslyExpensiveType;
begin
  SaveTime := Object.SomeHideouslyExpensiveProperty;
  SaveTime.A := X;
  SaveTime.B := Y;

is probably the best way.

--
Ray Lischner  (http://www.bardware.com)
co-author (with John Doyle) of Shakespeare for Dummies

Re:(dis)advantages of 'with'?


Quote
Stefan Hoffmeister wrote:
> >I found 'with' not working with some array types (of records), and therefore
> >would like to know more about the 'with' statement.

> It always works.

I'd hate to disagree... because it really depends on one's definition of
"works". However, I find it slightly annoying that I have to write:

procedure TNCCSGBranch.Assign(Source:TPersistent);

var
   SourceClass:TNetworkClass;

begin
if Assigned(Source) and (Source is TNCCSGBranch) then
begin
     FLeftNode.Free;
     FLeftNode:=nil;
     if Assigned((Source as TNCCSGBranch).FLeftNode) then
     begin
          SourceClass:=TNetworkClass((Source as
TNCCSGBranch).FLeftNode.ClassType);
          FLeftNode:=(SourceClass.Create as TNCCSGNode);
          FLeftNode.Assign((Source as TNCCSGBranch).FLeftNode);
     end;
end;
inherited Assign(Source);
end;

rather than something like:

procedure TNCCSGBranch.Assign(Source:TPersistent);

var
   SourceClass:TNetworkClass;

begin
if Assigned(Source) and (Source is TNCCSGBranch) then
begin
     with (Source as TNCCSGBranch) do
     begin
          Self.FLeftNode.Free;
          Self.FLeftNode:=nil;
          if Assigned(FLeftNode) then
          begin
               SourceClass:=TNetworkClass(FLeftNode.ClassType);
               Self.FLeftNode:=(SourceClass.Create as TNCCSGNode);
               Self.FLeftNode.Assign(FLeftNode);
          end;
     end;
end;
inherited Assign(Source);
end;

If you get my general drift.

Basically, I just wish the with statement would let one mask the
implicit "self" in all field and method declarations, instead  making
them apply to a checked typecast of another object...

Wierd, and perhaps not very good for the novice programmer, but it would
have saved me a lot of typing over the past few months!

MH.

--
Martin Harvey.
http://www.harvey27.demon.co.uk/mch24/
PGP key available from above address,
or http://wwwkeys.pgp.net/

Re:(dis)advantages of 'with'?


Quote
Martin Harvey wrote:
> procedure TNCCSGBranch.Assign(Source:TPersistent);

> var
>    SourceClass:TNetworkClass;

> begin
> if Assigned(Source) and (Source is TNCCSGBranch) then
> begin

    NSource:=TNCCSGBranch(Source);

This is what you want to do.

Quote

>      FLeftNode.Free;
>      FLeftNode:=nil;
>      if Assigned(NSource.FLeftNode) then
>      begin
>           SourceClass:=TNetworkClass(NSource.FLeftNode.ClassType);
>           FLeftNode:=(SourceClass.Create as TNCCSGNode);
>           FLeftNode.Assign(NSource.FLeftNode);
>      end;
> end;
> inherited Assign(Source);
> end;

Bob Lee

Re:(dis)advantages of 'with'?


In article <36BB99FE.8CBB8...@aziraphale.demon.co.uk>, Martin Harvey

Quote
<mar...@aziraphale.demon.co.uk> writes:
>If you get my general drift.

I didn't really Martin, I spent a good time trying to understand what you were
doing so that I could see the differences  -  and failed <g>. Could you give a
clearer example which would clarify your point ?

Quote
>Basically, I just wish the with statement would let one mask the
>implicit "self" in all field and method declarations, instead  making
>them apply to a checked typecast of another object...

I thought the "with" reference  was the compiler's first priority for a
qualifying prefix to resolve _all_ non-fully-qualified names, self or
otherwise. Not so ?

I would have liked Delphi to use a dot prefix to indicate that the "with"
reference should be used - that is one VB advantage <g>

In article <36c2067f.888...@news.rz.uni-passau.de>,

Quote
no.s...@address.in.signature (Stefan Hoffmeister) writes:
>I personally make a point NOT to extend a with over more than two or
>three lines of code and especially of NOT nesting with statements.

As with all "far" end; statements emcompassing more than about four lines of
code, I close off  with a comment, after the end; if used but including the
end; if not, eg :-

with MyObject do begin
.
. etc etc
.
end; {with MyObject}

or

{end; with MyObject}

 . . . but a "with" should only enclose lines where the majority of the
enclosed lines need a "with".

Alan Lloyd
alangll...@aol.com

Re:(dis)advantages of 'with'?


On Sat, 06 Feb 1999 01:25:18 +0000, Martin Harvey

Quote
<mar...@aziraphale.demon.co.uk> wrote:
>I'd hate to disagree... because it really depends on one's definition of
>"works". However, I find it slightly annoying that I have to write:
 ....
>if Assigned(Source) and (Source is TNCCSGBranch) then
>begin
>     FLeftNode.Free;
>     FLeftNode:=nil;
>     if Assigned((Source as TNCCSGBranch).FLeftNode) then
 ....
>rather than something like:
 ....
>if Assigned(Source) and (Source is TNCCSGBranch) then
>begin
>     with (Source as TNCCSGBranch) do
>     begin
>          Self.FLeftNode.Free;
>          Self.FLeftNode:=nil;
>          if Assigned(FLeftNode) then

(I hope I extracted the part you find annoying!)

The answer is:  you don't.  What you typed should be legal, assuming
that everything else in your code was fine.  

For example, this code works:

 procedure TForm1.Button1Click(Sender: TObject);
 begin
   with Sender as TButton do
     caption := 'It works';
 end;

The with statement takes an expression whose value is a record or
object.  "Sender as TButton" or "Source as TNCCSGBranch" are both
legal expressions.

Getting back to one of the early questions in the thread.  I think
someone answered this, but it's important, so I'll repeat it
differently:

  "with" is only executed once.  

It's not just a way to save typing, it actually fixes a particular
reference.  For example, you can say

  foo^.bar := 1;
  with foo^ do
  begin
     foo := somethingelse;
     bar := 2;
  end;

and both assignments to bar are interpreted as parts of the *original*
record that foo pointed to.  The fact that foo changed doesn't matter.

Duncan Murdoch

Re:(dis)advantages of 'with'?


Quote
Robert Lee wrote:

>     NSource:=TNCCSGBranch(Source);

> This is what you want to do.

Oh yeah. DOHHHH!!!

It won't even cause a performance loss, since LVA will spot it, and
it'll be optimised right out anyway!

MH.

--
Martin Harvey.
http://www.harvey27.demon.co.uk/mch24/
PGP key available from above address,
or http://wwwkeys.pgp.net/

Re:(dis)advantages of 'with'?


Im Artikel <36c52c5e.64277...@news.proaxis.com>, n...@junk.mail (Ray Lischner)
schreibt:

Quote
>WITH uses a hidden temporary variable.

Thanks, that simplifies my decisions.

DoDi

Other Threads