Jump to page: 1 24  
Page
Thread overview
Post increment and decrement
Mar 11, 2015
welkam
Mar 11, 2015
Ali Çehreli
Mar 11, 2015
welkam
Mar 12, 2015
deadalnix
Mar 11, 2015
mattcoder
Mar 11, 2015
mattcoder
Mar 12, 2015
Rikki Cattermole
Mar 12, 2015
Don
Mar 12, 2015
Rikki Cattermole
Mar 12, 2015
monarch_dodra
Mar 12, 2015
Nick Treleaven
Mar 12, 2015
Kagamin
Mar 14, 2015
Jonathan M Davis
Mar 14, 2015
Casper Færgemand
Mar 14, 2015
weaselcat
Mar 14, 2015
deadalnix
Mar 14, 2015
Ali Çehreli
Mar 15, 2015
Jonathan M Davis
Mar 15, 2015
Kagamin
Mar 15, 2015
ketmar
Mar 15, 2015
Kagamin
Mar 15, 2015
ketmar
Mar 20, 2015
welkam
Mar 20, 2015
deadalnix
Mar 20, 2015
welkam
Mar 20, 2015
deadalnix
Mar 21, 2015
welkam
Mar 23, 2015
Kagamin
Mar 23, 2015
krzaq
Mar 23, 2015
Kagamin
Mar 23, 2015
krzaq
Mar 15, 2015
deadalnix
Mar 22, 2015
Walter Bright
March 11, 2015
Observation Nr. 1
People prefer to write var++ instead of ++var.

Observation Nr. 2
Because of observation Nr. 1 and other reasons compilers became good at removing code that is not needed making var++ and ++var to produce the same code if returned value is not used.

Observation Nr. 3
Because of observation Nr. 2 more people use var++ in place where they really only need ++var.

Observation Nr. 4
Because of observation Nr. 3 people learning to program may mistakenly learn that var++ is just incrementing. (I am included in that list)

Observation Nr. 5
Because of observation Nr. 4 people can write slower than necessary code for classes with overloaded operator or even get bugs.

Because of all this why not make only one increment/decrement operator and have post increment/decrement to be called by template name, because it is a template?

template post_inc(T) {
auto tmp = T;
T++;
return tmp;
}
March 11, 2015
On 03/11/2015 10:23 AM, welkam wrote:

> Observation Nr. 1
> People prefer to write var++ instead of ++var.
>
> Observation Nr. 2
> Because of observation Nr. 1 and other reasons compilers became good at
> removing code that is not needed making var++ and ++var to produce the
> same code if returned value is not used.
>
> Observation Nr. 3
> Because of observation Nr. 2 more people use var++ in place where they
> really only need ++var.
>
> Observation Nr. 4
> Because of observation Nr. 3 people learning to program may mistakenly
> learn that var++ is just incrementing. (I am included in that list)
>
> Observation Nr. 5
> Because of observation Nr. 4 people can write slower than necessary code
> for classes with overloaded operator or even get bugs.

+5 for all your observations. (Ok, 5 each, 25 total. :p)

I cringe every time I see var++ and var--. That's why I advise against them:

  http://ddili.org/ders/d.en/arithmetic.html#ix_arithmetic.post-increment

> Because of all this why not make only one increment/decrement operator
> and have post increment/decrement to be called by template name, because
> it is a template?
>
> template post_inc(T) {
> auto tmp = T;
> T++;
> return tmp;
> }

Agreed but that train has already sailed (: having a good time today); there are too many programs out there doing var++.

Ali

March 11, 2015
I should write regex and go trough http://code.dlang.org/ to see how many of them  would break if we changed var++ implementation to ++var
March 11, 2015
On Wednesday, 11 March 2015 at 17:23:15 UTC, welkam wrote:
> Observation Nr. 1
> People prefer to write var++ instead of ++var.

Believe or not I always used pre-increment. But yes, I see the former in most of the codes out there.

Matheus.
March 11, 2015
On Wednesday, 11 March 2015 at 20:05:03 UTC, mattcoder wrote:
> On Wednesday, 11 March 2015 at 17:23:15 UTC, welkam wrote:
>> Observation Nr. 1
>> People prefer to write var++ instead of ++var.
>
> Believe or not I always used pre-increment. But yes, I see the former in most of the codes out there.

... and I'm with you! :)

Matheus.
March 12, 2015
On Wednesday, 11 March 2015 at 19:57:15 UTC, welkam wrote:
> I should write regex and go trough http://code.dlang.org/ to see how many of them  would break if we changed var++ implementation to ++var

No.
March 12, 2015
On 3/11/15 10:23 AM, welkam wrote:
> Observation Nr. 1
> People prefer to write var++ instead of ++var.
>
> Observation Nr. 2
> Because of observation Nr. 1 and other reasons compilers became good at
> removing code that is not needed making var++ and ++var to produce the
> same code if returned value is not used.
>
> Observation Nr. 3
> Because of observation Nr. 2 more people use var++ in place where they
> really only need ++var.
>
> Observation Nr. 4
> Because of observation Nr. 3 people learning to program may mistakenly
> learn that var++ is just incrementing. (I am included in that list)
>
> Observation Nr. 5
> Because of observation Nr. 4 people can write slower than necessary code
> for classes with overloaded operator or even get bugs.
>
> Because of all this why not make only one increment/decrement operator
> and have post increment/decrement to be called by template name, because
> it is a template?
>
> template post_inc(T) {
> auto tmp = T;
> T++;
> return tmp;
> }

Observation Nr. 6
Somebody didn't Read The Fine Manual. Page 369:

=========
If the result of a++ is not needed, the rewrite is ++a, which is subsequently rewritten to a.opUnary!"++"().
=========


Andrei

March 12, 2015
On 12/03/2015 1:50 p.m., Andrei Alexandrescu wrote:
> On 3/11/15 10:23 AM, welkam wrote:
>> Observation Nr. 1
>> People prefer to write var++ instead of ++var.
>>
>> Observation Nr. 2
>> Because of observation Nr. 1 and other reasons compilers became good at
>> removing code that is not needed making var++ and ++var to produce the
>> same code if returned value is not used.
>>
>> Observation Nr. 3
>> Because of observation Nr. 2 more people use var++ in place where they
>> really only need ++var.
>>
>> Observation Nr. 4
>> Because of observation Nr. 3 people learning to program may mistakenly
>> learn that var++ is just incrementing. (I am included in that list)
>>
>> Observation Nr. 5
>> Because of observation Nr. 4 people can write slower than necessary code
>> for classes with overloaded operator or even get bugs.
>>
>> Because of all this why not make only one increment/decrement operator
>> and have post increment/decrement to be called by template name, because
>> it is a template?
>>
>> template post_inc(T) {
>> auto tmp = T;
>> T++;
>> return tmp;
>> }
>
> Observation Nr. 6
> Somebody didn't Read The Fine Manual. Page 369:
>
> =========
> If the result of a++ is not needed, the rewrite is ++a, which is
> subsequently rewritten to a.opUnary!"++"().
> =========
>
>
> Andrei

+1
Compiler should work for you. This is one of those things it can rewrite to preference. During optimization.
March 12, 2015
On Thursday, 12 March 2015 at 04:06:14 UTC, Rikki Cattermole wrote:
> On 12/03/2015 1:50 p.m., Andrei Alexandrescu wrote:
>> On 3/11/15 10:23 AM, welkam wrote:
>>> Observation Nr. 1
>>> People prefer to write var++ instead of ++var.
>>>
>>> Observation Nr. 2
>>> Because of observation Nr. 1 and other reasons compilers became good at
>>> removing code that is not needed making var++ and ++var to produce the
>>> same code if returned value is not used.
>>>
>>> Observation Nr. 3
>>> Because of observation Nr. 2 more people use var++ in place where they
>>> really only need ++var.
>>>
>>> Observation Nr. 4
>>> Because of observation Nr. 3 people learning to program may mistakenly
>>> learn that var++ is just incrementing. (I am included in that list)
>>>
>>> Observation Nr. 5
>>> Because of observation Nr. 4 people can write slower than necessary code
>>> for classes with overloaded operator or even get bugs.
>>>
>>> Because of all this why not make only one increment/decrement operator
>>> and have post increment/decrement to be called by template name, because
>>> it is a template?
>>>
>>> template post_inc(T) {
>>> auto tmp = T;
>>> T++;
>>> return tmp;
>>> }
>>
>> Observation Nr. 6
>> Somebody didn't Read The Fine Manual. Page 369:
>>
>> =========
>> If the result of a++ is not needed, the rewrite is ++a, which is
>> subsequently rewritten to a.opUnary!"++"().
>> =========
>>
>>
>> Andrei
>
> +1
> Compiler should work for you. This is one of those things it can rewrite to preference. During optimization.

It doesn't even rely on the optimizer. This happens in the front-end, in the semantic pass.

March 12, 2015
On 12/03/2015 9:12 p.m., Don wrote:
> On Thursday, 12 March 2015 at 04:06:14 UTC, Rikki Cattermole wrote:
>> On 12/03/2015 1:50 p.m., Andrei Alexandrescu wrote:
>>> On 3/11/15 10:23 AM, welkam wrote:
>>>> Observation Nr. 1
>>>> People prefer to write var++ instead of ++var.
>>>>
>>>> Observation Nr. 2
>>>> Because of observation Nr. 1 and other reasons compilers became good at
>>>> removing code that is not needed making var++ and ++var to produce the
>>>> same code if returned value is not used.
>>>>
>>>> Observation Nr. 3
>>>> Because of observation Nr. 2 more people use var++ in place where they
>>>> really only need ++var.
>>>>
>>>> Observation Nr. 4
>>>> Because of observation Nr. 3 people learning to program may mistakenly
>>>> learn that var++ is just incrementing. (I am included in that list)
>>>>
>>>> Observation Nr. 5
>>>> Because of observation Nr. 4 people can write slower than necessary
>>>> code
>>>> for classes with overloaded operator or even get bugs.
>>>>
>>>> Because of all this why not make only one increment/decrement operator
>>>> and have post increment/decrement to be called by template name,
>>>> because
>>>> it is a template?
>>>>
>>>> template post_inc(T) {
>>>> auto tmp = T;
>>>> T++;
>>>> return tmp;
>>>> }
>>>
>>> Observation Nr. 6
>>> Somebody didn't Read The Fine Manual. Page 369:
>>>
>>> =========
>>> If the result of a++ is not needed, the rewrite is ++a, which is
>>> subsequently rewritten to a.opUnary!"++"().
>>> =========
>>>
>>>
>>> Andrei
>>
>> +1
>> Compiler should work for you. This is one of those things it can
>> rewrite to preference. During optimization.
>
> It doesn't even rely on the optimizer. This happens in the front-end, in
> the semantic pass.

In our implementation yes. But I'm emphasizing it doesn't have to.

« First   ‹ Prev
1 2 3 4