Bug 11303 – IsExpression can not return the correct result about function & delegate

Status
RESOLVED
Resolution
INVALID
Severity
critical
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
Windows
Creation time
2013-10-19T23:24:00Z
Last change time
2013-10-20T05:20:26Z
Assigned to
nobody
Creator
786325481

Comments

Comment #0 by 786325481 — 2013-10-19T23:24:48Z
Sorry for my poor English. I find that IsExpression can not return the correct result about function & delegate. Sample like that: unittest { static int test(int x) { return x * 2; } auto fn1 = &test; auto fn2 = (int x) { return x * 2; }; auto fn3 = (int x) { return x * 2; }; auto fn4 = (int x) => x * 2; auto fn5 = function (int x) => x * 2; auto fn6 = function (int x) { return x * 2; }; static assert(is(typeof(fn1) == function)); static assert(is(typeof(fn2) == function)); static assert(is(typeof(fn3) == function)); static assert(is(typeof(fn4) == function)); static assert(is(typeof(fn5) == function)); static assert(is(typeof(fn6) == function)); } IsExpression always return false, but the correct result is true. My environment : OS : Win8.1 Preview x64 Compiler: dmd2 2.063.2 & dmd2 2.064 beta IDE : Mono-D
Comment #1 by samukha — 2013-10-20T01:18:01Z
is(T == function) tests for function object type, not function pointer type. Many consider that an inconsistency, but that's the state of things. Your tests should be: static assert(is(typeof(*fn1) == function)); static assert(is(typeof(*fn2) == function)); static assert(is(typeof(*fn3) == function)); static assert(is(typeof(*fn4) == function)); static assert(is(typeof(*fn5) == function)); static assert(is(typeof(*fn6) == function));
Comment #2 by 786325481 — 2013-10-20T05:20:26Z
(In reply to comment #1) > is(T == function) tests for function object type, not function pointer type. > Many consider that an inconsistency, but that's the state of things. Your tests > should be: > > static assert(is(typeof(*fn1) == function)); > static assert(is(typeof(*fn2) == function)); > static assert(is(typeof(*fn3) == function)); > static assert(is(typeof(*fn4) == function)); > static assert(is(typeof(*fn5) == function)); > static assert(is(typeof(*fn6) == function)); Ohhhhhhhh... I got it. Thanks for your reply. I am reading the TDPL.In the last paragraph of Chapter 5.6,there is a little example code.It likes that: auto f = (int i) {}; assert(is(f == function)); So I write the tests above.