Change AliasSeq(0, ...) uses of foreach to static foreach#5729
Change AliasSeq(0, ...) uses of foreach to static foreach#5729dlang-bot merged 1 commit intodlang:masterfrom
Conversation
|
Thanks for your pull request, @wilzbach! Bugzilla referencesYour PR doesn't reference any Bugzilla issue. If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog. |
PetarKirov
left a comment
There was a problem hiding this comment.
LGTM, apart from some minor suggestions for std.datetime.
std/datetime/date.d
Outdated
| @@ -9858,23 +9858,17 @@ private int cmpTimeUnitsCTFE(string lhs, string rhs) @safe pure nothrow @nogc | |||
| import std.format : format; | |||
| import std.meta : AliasSeq; | |||
|
|
|||
There was a problem hiding this comment.
Changes to this module are a good candidate for a separate commit.
std/datetime/date.d
Outdated
| import std.meta : AliasSeq; | ||
|
|
||
| static string genTest(size_t index) | ||
| static assert(timeStrings.length == 10); |
There was a problem hiding this comment.
This assert is not necessary. It was there in the original code only because, the author didn't use foreach (n; staticIota!(0, timeStrings.length)) (e.g. staticIota from std.typecons).
|
|
||
| static string genTest(size_t index) | ||
| static assert(timeStrings.length == 10); | ||
| static foreach (i; 0 .. timeStrings.length) |
There was a problem hiding this comment.
Naming the indexes curr, next (cmp(t[curr], t[next]) == -1) and prev (cmp(t[curr], t[prev]) == 1) respectively would make the code a bit clearer.
|
@wilzbach: If you want to replace an existing |
MetaLang
left a comment
There was a problem hiding this comment.
I love how much nicer static foreach makes the mixin-heavy code look.
Also looks like CircleCI doesn't know about the feature yet.
Yup -> we need to update Dscanner: #5732 |
b2ba07a to
d6c9b5b
Compare
With #5732 finally being merged, the first use of |
std/math.d
Outdated
|
|
||
| foreach (x; AliasSeq!(smallP2, min_sub, X.min_normal, .25L, 0.5L, 1.0L, | ||
| 2.0L, 8.0L, pow(2.0L, X.max_exp - 1), bigP2)) | ||
| static foreach (x; [smallP2, min_sub, X.min_normal, .25L, 0.5L, 1.0L, |
There was a problem hiding this comment.
Why is this a static foreach to begin with?
| assert(!isPowerOf2(cast(X)-x)); | ||
| } | ||
|
|
||
| foreach (x; AliasSeq!(0.0L, 3 * min_sub, smallP7, 0.1L, 1337.0L, bigP7, X.max, real.nan, real.infinity)) |
There was a problem hiding this comment.
If aliasseq is not being used, can the import be removed also? (assuming that it is local import)
There was a problem hiding this comment.
The import is local, but it is used for the foreach (X; AliasSeq!(float, double, real)) above.
|
The fact that static foreach (i; 0 .. 3)
mixin("struct S"~i.to!string~" {}");Using more than one statement is quite a pain anyhow cause you need to escape any declarations with a unique name (or use Anyone thought about explicit exporting instead of explicit local @tgehr? static foreach (i; 0 .. 3)
{
immutable s = generateCode(i);
struct S { mixin(code); }
// just some syntax ideas
alias scope OuterName=S;
alias return OuterName=S;
alias export OuterName=S;
alias export : OuterName=S, OuterName2=S;
} |
|
@MartinNowak is there a reason that you can't just open up another scope? e.g.: |
|
@MartinNowak: Yes, the goal was to have the same behaviour for Note that However, if you want to use Another idea I had was to allow foreach(enum i; 0 .. 3)
{
alias _ = doSomeStuff!i; //Okay
}This is IMHO more natural if the goal is to unroll a loop at statement scope. |
Let's see what uses and hindrances for
Would be interesting, though as we can already do this with |
d6c9b5b to
97f6f39
Compare
|
Converted the |
I started to have a quick look at introducing more
static foreachat Phobos.Sadly even the naive idea to mark all
foreachloops which definitely are at Compile-Time - e.g. withAliasSeqdoesn't work:or in simpler words with a simplified example from Phobos (most of the uses of
foreach+AliasSeqare like this):https://run.dlang.io/gist/6ae32becc7ffaa1296a1a4752cebc294?compiler=dmd
And now with
static foreachthis fails due to the symbol being in the outer scope:FWIW Local Declarations would solve this and FYI @tgehr mentioned that he has a working implementation of
__localalready.