Thread overview
tdpl: function literals versus delegate lierals
Jan 19, 2012
Jerome BENOIT
Jan 19, 2012
Timon Gehr
Jan 19, 2012
bearophile
Jan 19, 2012
Manfred Nowak
January 19, 2012
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@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
January 19, 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@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 http://d.puremagic.com/issues/show_bug.cgi?id=3235

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


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.

January 19, 2012
Timon Gehr:

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

I'd like to see is expressions a bit re-designed instead of SIMDs added to DMD :-|

Bye,
bearophile
January 19, 2012
bearophile wrote:

> instead of SIMDs added

Most delevopers at the same time like to turf their old errors and are keen to do something knew.

-manfred