www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Fastest way to check if a predicate can take a single parameter of a

reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
I want to check whether

     auto _ = pred(T.init);
     static assert(is(typeof(_) == bool));

compiles or not.

Which one

     __traits(compiles, { auto _ = pred(T.init); static 
assert(is(typeof(_) == bool)); })

and

     is(typeof(pred(T.init)) == bool)

is preferred compilation performance-wise?
Oct 11 2019
parent Paul Backus <snarwin gmail.com> writes:
On Friday, 11 October 2019 at 09:05:25 UTC, Per Nordlöw wrote:
 I want to check whether

     auto _ = pred(T.init);
     static assert(is(typeof(_) == bool));

 compiles or not.

 Which one

     __traits(compiles, { auto _ = pred(T.init); static 
 assert(is(typeof(_) == bool)); })

 and

     is(typeof(pred(T.init)) == bool)

 is preferred compilation performance-wise?
Seems like the second one requires strictly less semantic analysis and is shorter and easier to read, so I'd go with that. Note that `pred(T.init)` will fail if pred accepts its argument by ref. If you want to handle that case, you have to do something like is(ReturnType!((T arg) => pred(arg)) == bool))
Oct 11 2019