Advent of D

Steven Schveighoffer schveiguy at yahoo.com
Tue Mar 6 20:37:34 UTC 2018


On 3/6/18 1:09 PM, Jordi Gutiérrez Hermoso wrote:
> I wrote a blog post about working on Advent of Code in D. You can read 
> it here:
> 
> http://jordi.inversethought.com/blog/advent-of-d/

I'm enjoying this.

One thing jumped out at me: static does not mean execute at compile time 
(exactly), it really means allocate this data in thread-local storage.

What triggers the compile-time execution is the fact that static 
*initializers* need to be decided at compile time.

So while it is executing the regex building at compile time in your 
example, there are other ways to do it, and in this case, I'd prefer 
using an enum. You can also use ctRegex.

The drawback of using static is that it keeps its value between function 
calls:

int bar() { return 42; }

void foo()
{
    static x = bar();
    x += 5;
    writeln(x);
}

void main()
{
    foo(); // 47
    foo(); // 52
}

So it may not be what you are looking for.

Whereas if you use:

void foo()
{
     enum x = bar();
     writeln(x + 5);
}

it will always write the same answer (and not allocate global space for it).

BTW, that tour page needs updating, it has a few errors in it.

-Steve


More information about the Digitalmars-d mailing list