Implicit casting of int enum members to int

Mike Bierlee via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Oct 2 11:31:11 PDT 2016


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.

Is this a bug in D? Or is using an int as base type the same as 
having no base type at all?


More information about the Digitalmars-d-learn mailing list