Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "!" before a function do in D

Tags:

d

I'm trying to learn some D for a project and frequently see code such as

    foreach (i; 0 .. 10)
        benchmark!test(1)[0]
            .to!("msecs", double)
            .reverseArgs!writefln
                (" took: %.2f miliseconds");

I'm uncertain on why some function calls have ! before them while others do not.

like image 579
Kurt Wimer Avatar asked Mar 13 '26 16:03

Kurt Wimer


1 Answers

Well, ! BEFORE a function inverts the result, just like if(!a). But ! AFTER a function separates it from its compile-time (template) arguments, like to!int. The parenthesis on those arguments are optional iff it is one simple word, so like benchmark!test or to!int, but if it is more than that, it needs parens like to!("msecs", double).

In a lot of cases, a function is passed like reverseArgs!writefln which just passed the writefln function as a compile-time argument to the reverseArgs template/function.

So in general: foo!(compile, time, args)(run, time, args) where not all functions have !(compile, time args).

Does that make sense? I can try to edit if not...

like image 128
Adam D. Ruppe Avatar answered Mar 16 '26 08:03

Adam D. Ruppe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!