Bug 5359 – std.traits.isDelegate should work for types and expressions

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-12-19T14:49:00Z
Last change time
2013-03-09T16:14:08Z
Keywords
pull
Assigned to
andrej.mitrovich
Creator
andrej.mitrovich

Comments

Comment #0 by andrej.mitrovich — 2010-12-19T14:49:57Z
import std.traits; import std.stdio : writeln; void main() { int delegate() dg; writeln(typeid(dg)); // "int delegate()" assert(isDelegate!(int delegate())); // ok assert(isSomeFunction!(dg)); // ok.. assert(isDelegate!(dg)); // fails } Huh?
Comment #1 by issues.dlang — 2010-12-19T15:33:16Z
I believe that there are at least a couple of other open bugs on traits and delegates, so it would appear that traits are very broken for delegates right now.
Comment #2 by samukha — 2010-12-20T04:28:28Z
I am sure that using homonym templates for testing types and expressions is a bad idea. It results in some syntactic compression but at the same time brings lots of confusion. There should be two distinct templates. Something like this: template isExpression(alias expression) { enum isExpression = is(typeof(expression)); } template isDelegate(alias expression) if (isExpression!expression) { enum isDelegate = isDelegateType!(typeof(expression)); } template isDelegateType(T) { static enum isDelegateType = is(T == delegate); } template isFunctionPointer(alias expression) if (isExpression!expression) { enum isFunctionPointer = isFunctionPointerType!(typeof(expression)); } template isFunctionPointerType(T) { static if (__traits(compiles, *T.init)) enum isFunctionPointerType = isFunctionType!(typeof((*T.init))); else enum isFunctionPointerType = false; } template isFunctionType(T) { enum isFunctionType = is(T == function); } unittest { alias void delegate() Dg; Dg dg; alias void function() Fn; Fn fn; static void foo() { } static assert(isDelegate!dg); static assert(isDelegateType!Dg); static assert(!__traits(compiles, isDelegate!Dg)); static assert(!isDelegateType!Fn); static assert(isFunctionPointer!fn); static assert(!__traits(compiles, isFunctionPointer!Fn)); static assert(!isFunctionType!Fn); static assert(isFunctionPointerType!Fn); static assert(!isFunctionPointerType!Dg); static assert(isFunctionType!(typeof(*&foo))); static assert(!isFunctionType!Fn); static assert(!isFunctionType!Dg); }
Comment #3 by samukha — 2010-12-20T06:02:00Z
"static enum" should be replaced with "enum" in the example
Comment #4 by andrej.mitrovich — 2013-02-24T16:24:20Z
The real issue here was that isDelegate should have only accepted types, IOW this should have been a compile-time error not a runtime one. Fixed with https://github.com/D-Programming-Language/phobos/pull/1164
Comment #5 by andrej.mitrovich — 2013-03-08T13:26:36Z
Comment #6 by github-bugzilla — 2013-03-09T15:59:05Z
Commits pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/091a57650b5cd51cf0912fdee488d5c0730c7e06 Fixes Issue 5359 - isDelegate should work for types and expressions. https://github.com/D-Programming-Language/phobos/commit/28fad77612f8252658ced4989105271b4f3717a0 Merge pull request #1192 from AndrejMitrovic/Fix5359 Issue 5359 - isDelegate should work for types and expressions.