tdpl: function literals versus delegate lierals

Timon Gehr timon.gehr at
Thu Jan 19 09:21:40 PST 2012

On 01/19/2012 05:41 PM, Jerome BENOIT wrote:
> Hello List:
> On my box, the following D source, inspired by the subsection 5.6.1 of
> tDpl,
> does not work as expected:
> -----------------------------------------------------------------
> // adhoc_06.d
> import std.stdio;
> unittest {
> // Tersest, most convenient code
> auto f = (int i) {};
> writeln(typeid(f));
> assert(is(f == function));
> }
> void main() {}
> -----------------------------------------------------------------
> I get:
> void delegate()
> core.exception.AssertError at adhoc_06.d(7): unittest failure
> According to the book, the assertion is true and f is a function
> but not a literal.
> What is going wrong ?
> Thanks in advance,
> Jerome

Many things, actually. You are looking at both an error in TDPL and a 
compiler bug. The compiler bug is already fixed in git head and will not 
exist in the next release. See

In the line:

auto f = (int i) {};

f is deduced as void delegate(int) pure nothrow @safe instead of as void 
function(int) pure nothrow @safe. This is the compiler bug that has been 

In the line:

assert(is(f == function));

TDPL contains an error. Is expressions can be used to query some 
properties of types. If an involved type is not a well-formed type the 
result is false. Since f is a variable and not a type, the is expression 
yields false. is(T == function) tests whether or not T is a function 
type. Therefore, the line should actually read is(typeof(*f)==function), 
as f is a function pointer.

I am not very happy about this particular quirk of is expressions:

void delegate() dg; // declares a delegate
void function() fp; // declares a function _pointer_

assert( is(typeof(dg) == delegate));
assert(!is(typeof(fp) == function)); // the is expression tests whether 
it is a function, not whether it is a function pointer
assert(is(typeof(*fp) == function));

You may want to use std.traits.IsFunctionPointer and 
std.traits.IsDelegate instead.

More information about the Digitalmars-d-learn mailing list