Albert van der Horst wrote:
> Peter Nilsson <> wrote:
> > Albert van der Horst wrote:
> > > BRG <> wrote:
> > > > Does anyone here know whether the assignment:
> > > >
> > > > a[i] = x[ a[i] ]++;
> > >
> > > I never use a statement like
> > > x++;
> > > on its own.
> >
> > Why?!
> >
> > > I always do the equivalent (barring the result value)
> > > x += 1;
> > > This shows that it is an assignment, rather than a side effect.
> >
> > Rather poor reasoning since x++ is idiomatic, and because _all_
> > statements are _only_ evaluated for their side effects,
> > assignments or otherwise (e.g. function calls.)
>
> You use side-effect in a politically correct way here.
It's a term defined in the standard...
"Accessing a volatile object, modifying an object, modifying a
file, or calling a function that does any of those operations
are all _side effects_, ..."
> I thought the context made clear that is not what I say.
> In an expression like (i + j++)
> two thing are happening with j, getting its value and the
> incrementation which I call a side-effect. (And the use is
> not improper, because it is indeed a lasting effect.).
I (think) I understand that. I was pointing out that C turned
assignments and function calls into expression operators. [It's
not the only language to do this, but the consequences do
surprise many people who program other languages.]
> > > (The result of x++ is of course x, then it is thrown away,
> > > oh and by the way, I have incremented x for you.
> > > Extremely silly.)
> >
> > Yes, ": @++ dup @ dup 1+ over ! ;" _is_ silly, but only because
> > Forth is silly. 
>
> This leaves a pointer to x and has its value incremented.
Drat! I meant ": @++ dup @ dup 1+ rot ! ;"
> It could be simplified to: @++ 1 over +! ;
I was hoping you would simplify it, although I didn't anticipate
getting my version wrong. ;( But I'm pretty sure what I was
aiming for could be simplified to... : @++ 1 over +! @ ;
Anyway, my point is that, to a Forth programmer, the optimisation
seems obvious. Within C, the same 'obviousness' surrounds the
optimisation of x += 1 to x++; or ++x;.
> It is not equivalent to the c-code x += 1;
It was meant to be equivalent to x++;, i.e. (adr -- n) where
adr is the lvalue (address) of x.
> Lets discuss @+ which leaves an incremented address
> and a content, the equivalent of *p++ in c.
>
> In the context of Forth it is indeed silly to
> do
> @+ DROP
> instead of just
> @
> This is analogous.
In C it depends on the context: *p++; is silly, however
c = *p++; or f(*p++); isn't.
> In the first example you calculate
> two results, then throw one away. Instead of just calculating
> a result.
>
> But more on topic.
> Is my
> x += 1;
> sufficiently un-idiomatic to get some eyes browsed?
As Keith points out, there are subjective cases where it's valid,
but in general it will raise eyebrows.
It will make the reader think that you're entirely clued up
in C, just as my posted attempt at Forth was obviously naive
from a Forth perspective.
> What about
> p += sizeof(struct x);
[Again See Keith's comment, but also....]
Note that sizeof(struct x) cannot generally be expected to be 1.
There is no unary auto-increment-by-something-other-than-1
operator in C. So, p += 2; is not un-idiomatic.
--
Peter