WITH statement
rcd at nbires.UUCP
rcd at nbires.UUCP
Mon Jul 21 16:25:09 AEST 1986
> The ambiguous condition scares me too, and I think I could live
> much better with being able to take the address of the thing
> and then using it, like:
>
> recpointer := &recarray[selector];
> ...
> recpointer^.foo := 1;
> recpointer^.bar := 2;
I don't intend to flame, but there's a funny ring to this sort of view of
the (Pascal) `with' statement. C programmers roundly criticize Pascal as
being more cumbersome in notation. I generally agree; Pascal could allow
more terse expression without suffering...but then why is it scary when
Pascal has a construct which allows more terse expression than C?
First off, the standard disclaimer: the `with' statement can be abused, as
can any statement (including the null statement!) in any language.
The `with' statement is an interesting construct from a programming
language standpoint; I don't know of too many others in its class. One way
to think of it is as a procedure:
with p^, q[i] do...
is like starting a procedure right there with (value) parameters being the
records given. Within the procedure you have access to these objects which
is more convenient and presumably more efficient. It's a funny open-code
sort of thing.
Opening a with using two records of the same type is possible, but rare
because it's kind of silly. (It has nowhere near the potential for screwup
of, say, calling a procedure with two pointer parameters pointing to the
same object.)
The "scary" part of the `with' statement--the loss of qualifier in front of
field selectors--is no problem at all in practice because `with' statements
tend to be intensive activity on the records being manipulated. I've
observed that `with' statements tend to be either fairly short (probably
under 10 lines) or the entirety of a procedure where the `with' opens one
or more of the procedure's parameters--for example:
procedure xxx(p: pwhatzy);
...decls
begin with p^ do begin
...work on the object referenced by p
end end;
I've written a lot of Pascal and a lot more C. Mostly I find C faster to
write, but when I start writing a sequence of code to fill in a structure,
I sure miss the `with' statement.
--
Dick Dunn {hao,ucbvax,allegra}!nbires!rcd (303)444-5710 x3086
...At last it's the real thing...or close enough to pretend.
More information about the Comp.lang.c
mailing list