D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 17444 - std.traits.isCallable doesn't recognize struct member function if return type is 'auto'.
Summary: std.traits.isCallable doesn't recognize struct member function if return type...
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P1 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-05-27 11:01 UTC by Dario Schiavon
Modified: 2022-11-18 15:23 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Dario Schiavon 2017-05-27 11:01:55 UTC
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 uplink.coder 2017-05-27 11:06:50 UTC
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 Dario Schiavon 2017-05-27 11:15:21 UTC
(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 uplink.coder 2017-05-27 11:23:38 UTC
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 RazvanN 2022-11-18 15:23:11 UTC
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.