Bug 16586 – Implicit casting of enum with explicit int base type fails
Status
RESOLVED
Resolution
WORKSFORME
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Windows
Creation time
2016-10-03T22:08:43Z
Last change time
2022-11-10T14:40:26Z
Assigned to
No Owner
Creator
m.bierlee
Comments
Comment #0 by m.bierlee — 2016-10-03T22:08:43Z
Consider the following code:
enum StringTypeEnumOne : string {
bla = "bla"
}
enum StringTypeEnumTwo : string {
bleh = "bleh"
}
enum IntTypeEnumOne : int {
bla = 1
}
enum IntTypeEnumTwo : int {
bleh = 2
}
public void main() {
string[] strings = [StringTypeEnumOne.bla, StringTypeEnumTwo.bleh];
int[] ints = [IntTypeEnumOne.bla, IntTypeEnumTwo.bleh];
}
When compiled the following compilation error is thrown:
src\app.d(19,16): Error: cannot implicitly convert expression (cast(IntTypeEnumOne)1) of type IntTypeEnumOne to IntTypeEnumTwo
The string members are implicitly cast just fine, however I also expected the members of the int enum to be cast implicitly because I explicitly defined the base type of the enum.
Comment #1 by m.bierlee — 2016-10-03T22:10:23Z
Current work-around is to explicitly cast all enum members:
int[] ints = [cast(int) IntTypeEnumOne.bla, cast(int) IntTypeEnumTwo.bleh];
Comment #2 by razvan.nitu1305 — 2022-11-10T14:40:26Z
I cannot reproduce this. It seems to have been fixed since 2.094.2