Bye bye, fast compilation times
Steven Schveighoffer
schveiguy at yahoo.com
Tue Feb 6 19:31:24 UTC 2018
On 2/6/18 2:07 PM, H. S. Teoh wrote:
> I'm not sure I'm seeing the value of using ctRegex here. What's wrong
> with a module static runtime regex initialized by a static this()?
No, I'd rather have it initialized on first call.
>
> And before anyone complains about initializing the regex if user code
> never actually uses it, it's possible to use static this() on an
> as-needed basis:
>
> template ipRegex()
> {
> // Eponymous templates FTW!
> Regex!char ipRegex;
>
> static this()
> {
> ipRegex = regex(`blah blah blah`);
> }
> }
>
> auto isEmail(... blah blah ...)
> {
> ...
> if (ipRegex.match(...)) ...
> ...
> }
>
> Basically, if `ipRegex` is never referenced, the template is never
> instantiated and the static this() basically doesn't exist. :-D
> Pay-as-you-go FTW!
You may not realize that this actually compiles it for ALL modules that
use it, and the compiler puts in a gate to prevent it from running more
than once. So you pay every time anyways (compile-time wise at least).
It also makes any importing module now a module that defines a static
ctor, so cycles are much more likely.
In any case, there is a PR in the works that should eliminate the need
for regex altogether: https://github.com/dlang/phobos/pull/6129
-Steve
More information about the Digitalmars-d
mailing list