Bug 17444 – std.traits.isCallable doesn't recognize struct member function if return type is 'auto'.

Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2017-05-27T11:01:55Z
Last change time
2022-11-18T15:23:11Z
Assigned to
No Owner
Creator
Dario Schiavon

Comments

Comment #0 by dario.schiavon — 2017-05-27T11:01:55Z
import std.traits : isCallable; struct MyStruct { auto foo() { return MyStruct(); } static if (isCallable!foo) pragma(msg, "GOOD"); else pragma(msg, "BAD"); } Prints "GOOD" if foo returns 'MyStruct' instead of 'auto'. Member function foo is actually usable in both cases. I'm unsure whether this is a Phobos problem or a compiler problem. Using DMD32 v2.074.0 or LDC2 v1.1.1 (which is based on DMD v2.071.2).
Comment #1 by uplink.coder — 2017-05-27T11:06:50Z
This has to do with the order of symbol resolving. You will see that it will print good, if you put the static if outside of the struct body.
Comment #2 by dario.schiavon — 2017-05-27T11:15:21Z
(In reply to uplink.coder from comment #1) > This has to do with the order of symbol resolving. > You will see that it will print good, if you put the static if outside of > the struct body. Thanks for the prompt answer. Do you mean that this is the intended behavior, or is it still a bug? I'll explain what I'm trying to achieve: a mixin template that will automatically define a member function foo if it is not already present. However, the fact that it works in some situations and not in others is mildly annoying. Maybe you can suggest a better approach?
Comment #3 by uplink.coder — 2017-05-27T11:23:38Z
I would classify this as a bug, however it seems like a fix for this would break other situations. for now the best workaround is to not use functions returning auto inside that struct.
Comment #4 by razvan.nitu1305 — 2022-11-18T15:23:11Z
I cannot reproduce this. The code in the bug report prints "GOOD" in both situations (return type "auto" or return type "MyStrut"). Closing as WORKSFORME.