Kurt Wimer
Kurt Wimer

Reputation: 21

What does "!" before a function do in 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.

Upvotes: 2

Views: 214

Answers (1)

Adam D. Ruppe
Adam D. Ruppe

Reputation: 25615

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...

Upvotes: 8

Related Questions