-
-
Notifications
You must be signed in to change notification settings - Fork 706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change AliasSeq(0, ...) uses of foreach to static foreach #5729
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes to this module are a good candidate for a separate commit.
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; | |||
|
|
|||
| static string genTest(size_t index) | |||
| static assert(timeStrings.length == 10); | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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).
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; | |||
|
|
|||
| static string genTest(size_t index) | |||
| static assert(timeStrings.length == 10); | |||
| static foreach (i; 0 .. timeStrings.length) | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
| @@ -8154,14 +8154,14 @@ if (isNumeric!X) | |||
| { | |||
| immutable min_sub = X.min_normal * X.epsilon; | |||
|
|
|||
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this a static foreach to begin with?
| { | ||
| assert( isPowerOf2(cast(X) x)); | ||
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.