Generating switch at Compile Time
Jesse Phillips via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Apr 17 12:29:35 PDT 2017
On Thursday, 13 April 2017 at 21:33:28 UTC, ag0aep6g wrote:
> That's not a static foreach. It's a normal run-time foreach.
> The switch jumps into the loop body without the loop head ever
> executing. The compiler is correct when it says that
> initialization of li is being skipped.
Good good. I did miss that as I was trying different things.
> Make `list` an enum or alias instead. Then the foreach is
> unrolled at compile time, you don't get a deprecation message,
> and it works correctly.
Yes it did.
> By the way, in my opinion, `case li[0]:` shouldn't compile with
> the static immutable `list`. `list` and `li[0]` are dynamic
> values. The compiler only attempts (and succeeds) to evaluate
> them at compile time because they're typed as immutable. The
> way I see it, that only makes things more confusing.
This is very interesting. I wonder if the compiler is still
unrolling the loop at compile time since it functions as
expected; It certainly needs that deprecation though.
More information about the Digitalmars-d-learn
mailing list