Understand typeof trick

Joakim Brännström via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Dec 25 07:36:50 PST 2015

On Friday, 25 December 2015 at 14:55:04 UTC, anonymous wrote:
> On 25.12.2015 13:10, Joakim Brännström wrote:
>> [B]
>> Evaluates to the function type "constructed" by binaryFun.
> Almost. It evaluates to the type of the expression. The 
> expression is a function call, so typeof evaluates to the 
> return type of the generated function.

Ahh, I missed this.
The subtle difference between:
int fun(uint x) { return 1; }
pragma(msg, typeof(fun));   // -> int(uint x)
pragma(msg, typeof(fun());  // -> int

> That second one leads us to the longest form of the is-typeof 
> idiom: `is(typeof({foo(); bar();}))`. Wrapping the code in a 
> delegate so that it's an expression, which can be passed to 
> typeof.

Nice, didn't know that was possible. I'll remember that.

Thank you for the detailed answer.

More information about the Digitalmars-d-learn mailing list