www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - tdpl: function literals versus delegate lierals

reply Jerome BENOIT <g6299304p rezozer.net> writes:
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
Jan 19 2012
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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.
Jan 19 2012
parent reply bearophile <bearophileHUGS lycos.com> writes:
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
Jan 19 2012
parent Manfred Nowak <svv1999 hotmail.com> writes:
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
Jan 19 2012