On Sunday, October 02, 2016 18:31:11 Mike Bierlee via Digitalmars-d-learn 
wrote:
> 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?

It looks like a bug. In theory, when you create an array literal like that,
it should choose the common type of all of the values in it (and result in a
compilation error if that doesn't work), though I recall there being bugs
with that and classes (with it just selecting the type of the last value
IIRC), so it doesn't entirely surprise me if you're seeing bugs with it. If
both of the values were from the same enum, I would expect that the
resulting type would be a dynamic array of that enum type, and the
initialization of ints would then fail, but with them being different enum
types with the base type of int, it really should result in int[], which
would succeed. So, it does look like you've found a bug, and the fact that
it works with enum types with a base type of string but not int is
particularly bizarre.

- Jonathan M Davis

Reply via email to