Jump to page: 1 2 3
Thread overview
Is old style compile-time foreach redundant?
Jan 06, 2018
Ali Çehreli
Jan 07, 2018
Simen Kjærås
Jan 07, 2018
Stefan Koch
Jan 07, 2018
H. S. Teoh
Jan 07, 2018
Stefan Koch
Jan 12, 2018
Seb
Jan 07, 2018
Ali Çehreli
Jan 07, 2018
Seb
Jan 07, 2018
Ali Çehreli
Jan 07, 2018
Ali Çehreli
Jan 08, 2018
H. S. Teoh
Jan 09, 2018
H. S. Teoh
Jan 09, 2018
H. S. Teoh
Jan 09, 2018
H. S. Teoh
Jan 09, 2018
Timon Gehr
Jan 09, 2018
H. S. Teoh
Jan 10, 2018
Seb
Jan 08, 2018
Jonathan M Davis
January 06, 2018
Is 'static foreach' sufficient for all needs or is there any value for regular foreach over compile-time sequences?

Code unrelated to the question:

import std.stdio;

void main() {
    // Old style compile-time foreach. This still works
    // when 'static' is uncommented below.
    import std.meta : AliasSeq;
    /* static */ foreach (i; AliasSeq!(1, "hello", 2)) {
        writeln(i);
    }

    // Proper 'static foreach'.
    import std.range : iota;
    import std.algorithm : map;
    static foreach (i; 3.iota.map!(a => a * 10)) {
        writeln(i);
    }
}

Ali

January 07, 2018
On Saturday, 6 January 2018 at 23:25:58 UTC, Ali Çehreli wrote:
> Is 'static foreach' sufficient for all needs or is there any value for regular foreach over compile-time sequences?

There is, as far as I've seen, there's nothing old foreach-over-tuple can do that new-style static foreach can't. The big difference is static foreach doesn't introduce a scope, which is great when that's what you want, and only requires an additional set of braces otherwise.

In some cases, especially when combined with mixins, this new behavior might manifest bugs at some later point. Simplified example:

mixin template declareInts(names...) {
    static foreach (name; names) {
        mixin("int "~name~";");
    }
}

As long as you use it with non-duplicate names, this will work great. It will fail the moment a junior programmer decides he wants two ints with the same name. Real-world examples will of course be more involved.

Old foreach is however not going away any time soon - there's far too many codebases out there that use the feature.

--
  Simen
January 07, 2018
On Saturday, 6 January 2018 at 23:25:58 UTC, Ali Çehreli wrote:
> Is 'static foreach' sufficient for all needs or is there any value for regular foreach over compile-time sequences?
>
> Code unrelated to the question:
>
> import std.stdio;
>
> void main() {
>     // Old style compile-time foreach. This still works
>     // when 'static' is uncommented below.
>     import std.meta : AliasSeq;
>     /* static */ foreach (i; AliasSeq!(1, "hello", 2)) {
>         writeln(i);
>     }
>
>     // Proper 'static foreach'.
>     import std.range : iota;
>     import std.algorithm : map;
>     static foreach (i; 3.iota.map!(a => a * 10)) {
>         writeln(i);
>     }
> }
>
> Ali

No it's not.
When you can use the old style do so. Since it puts less stress on the compiler in the general case.
January 06, 2018
On Sun, Jan 07, 2018 at 12:55:27AM +0000, Stefan Koch via Digitalmars-d-learn wrote:
> On Saturday, 6 January 2018 at 23:25:58 UTC, Ali Çehreli wrote:
> > Is 'static foreach' sufficient for all needs or is there any value for regular foreach over compile-time sequences?
[...]
> No it's not.
> When you can use the old style do so. Since it puts less stress on the
> compiler in the general case.

Really? Based on a recent post by Jonathan Davis, the new static foreach actually runs faster in certain use cases.


T

-- 
No! I'm not in denial!
January 06, 2018
On 01/06/2018 04:55 PM, Stefan Koch wrote:

> When you can use the old style do so. Since it puts less stress on the
> compiler in the general case.

My question is related to how to update my book. If the difference is mainly about performance, I think I will cover 'static foreach' but also mention old style foreach as a note.

Ali

January 07, 2018
On Sunday, 7 January 2018 at 01:08:44 UTC, H. S. Teoh wrote:
> On Sun, Jan 07, 2018 at 12:55:27AM +0000, Stefan Koch via Digitalmars-d-learn wrote:
>> On Saturday, 6 January 2018 at 23:25:58 UTC, Ali Çehreli wrote:
>> > Is 'static foreach' sufficient for all needs or is there any value for regular foreach over compile-time sequences?
> [...]
>> No it's not.
>> When you can use the old style do so. Since it puts less stress on the
>> compiler in the general case.
>
> Really? Based on a recent post by Jonathan Davis, the new static foreach actually runs faster in certain use cases.
>
>
> T

That might be true.
If you are hitting some constant factor, however the big-o for static foreach is worse then for tuple foreach.
January 07, 2018
On Sunday, 7 January 2018 at 01:52:10 UTC, Ali Çehreli wrote:
> On 01/06/2018 04:55 PM, Stefan Koch wrote:
>
> > When you can use the old style do so. Since it puts less
> stress on the
> > compiler in the general case.
>
> My question is related to how to update my book. If the difference is mainly about performance, I think I will cover 'static foreach' but also mention old style foreach as a note.
>
> Ali

How about doing sth. similar like for DIP1003?

https://dlang.org/spec/contracts.html#pre_post_contracts

> Since DIP1003 has been applied the actual function body starts with do. In the past, body was used, and could still be encountered in old code bases.
January 06, 2018
On 01/06/2018 06:20 PM, Seb wrote:

> How about doing sth. similar like for DIP1003?
> 
> https://dlang.org/spec/contracts.html#pre_post_contracts

Already done! :)


https://bitbucket.org/acehreli/ddili/commits/2f10c048c2940a49263319d0c23b0ad661449f3e

Ali
January 06, 2018
On 01/06/2018 10:53 PM, Ali Çehreli wrote:
> On 01/06/2018 06:20 PM, Seb wrote:
> 
>> How about doing sth. similar like for DIP1003?
>>
>> https://dlang.org/spec/contracts.html#pre_post_contracts
> 
> Already done! :)
> 
> 
> https://bitbucket.org/acehreli/ddili/commits/2f10c048c2940a49263319d0c23b0ad661449f3e 

What I meant is, yes, I will do it similar to the body-do change as you suggested.

Ali
January 07, 2018
On 1/6/18 6:25 PM, Ali Çehreli wrote:
> Is 'static foreach' sufficient for all needs or is there any value for regular foreach over compile-time sequences?

If you use continues or breaks, then you need to switch to gotos if using static foreach, as it does not support them directly.

-Steve
« First   ‹ Prev
1 2 3