D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 21174 - Recognize string value from string enum
Summary: Recognize string value from string enum
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-08-18 13:16 UTC by apham
Modified: 2020-08-18 13:27 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description apham 2020-08-18 13:16:56 UTC
enum E
{
	text1 = "text1",
	text2  = "text2",
}

enum E2 : string
{
	text1 = "text1",
	text2  = "text2",
}

struct S
{
@safe:

public:
    this(T)(T value) nothrow
    if (is(T == bool) || is(T == int) || is(T == string))
    {}
}

void main()
{
    int a = 1;
    
    // Not work 
    //cannot deduce function from argument types !()(E)
    auto s = S(a == 1 ? E.text1 : E.text2); 
    //cannot deduce function from argument types !()(E2)
    auto s2 = S(a == 1 ? E2.text1 : E2.text2); 
    
    //work
    string t = a == 1 ? E.text1 : E.text2;
    auto s3 = S(t); 
}
Comment 1 Simen Kjaeraas 2020-08-18 13:27:44 UTC
E and E2 are different types from string, so when you check is(T == string), you're explicitly disallowing them.

is(T1 == T2) is a lot stricter than calling fun(int) with a short value - it asks 'are these types the same type?', and E and string simply are not.

You may have wanted to write is(T : string), which checks if E is implicitly convertible to string, which it is. This compiles and runs.