Bug 21174 – Recognize string value from string enum
Status
RESOLVED
Resolution
INVALID
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-08-18T13:16:56Z
Last change time
2020-08-18T13:27:44Z
Assigned to
No Owner
Creator
apham
Comments
Comment #0 by apz28 — 2020-08-18T13:16:56Z
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 by simen.kjaras — 2020-08-18T13:27:44Z
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.