D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 16586 - Implicit casting of enum with explicit int base type fails
Summary: Implicit casting of enum with explicit int base type fails
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Windows
: P1 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-10-03 22:08 UTC by m.bierlee
Modified: 2022-11-10 14:40 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 m.bierlee 2016-10-03 22:08:43 UTC
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 m.bierlee 2016-10-03 22:10:23 UTC
Current work-around is to explicitly cast all enum members:
int[] ints = [cast(int) IntTypeEnumOne.bla, cast(int) IntTypeEnumTwo.bleh];
Comment 2 RazvanN 2022-11-10 14:40:26 UTC
I cannot reproduce this. It seems to have been fixed since 2.094.2