digitalmars.D.learn - Implicit casting of int enum members to int
- Mike Bierlee (26/26) Oct 02 2016 Consider the following code:
- Jonathan M Davis via Digitalmars-d-learn (15/41) Oct 03 2016 It looks like a bug. In theory, when you create an array literal like th...
- Mike B Johnson (9/10) Jun 04 2017 Is this bug ever going to be fixed?
- H. S. Teoh via Digitalmars-d-learn (10/24) Jun 04 2017 Works for me:
- Mike Bierlee (4/7) Jun 05 2017 I've filed this issue under
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?
Oct 02 2016
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
Oct 03 2016
On Monday, 3 October 2016 at 09:21:37 UTC, Jonathan M Davis wrote:[...]Is this bug ever going to be fixed? I can't do enum X : Y { a = 1 } because 1 is not implicitly convertible to Y, even though Y is aliased to an int so it should be.
Jun 04 2017
On Mon, Jun 05, 2017 at 01:23:22AM +0000, Mike B Johnson via Digitalmars-d-learn wrote:On Monday, 3 October 2016 at 09:21:37 UTC, Jonathan M Davis wrote:Works for me: alias Y = int; enum X : Y { a = 1 } Please post a minimal example that doesn't work. T -- Try to keep an open mind, but not so open your brain falls out. -- theboz[...]Is this bug ever going to be fixed? I can't do enum X : Y { a = 1 } because 1 is not implicitly convertible to Y, even though Y is aliased to an int so it should be.
Jun 04 2017
On Monday, 5 June 2017 at 01:23:22 UTC, Mike B Johnson wrote:On Monday, 3 October 2016 at 09:21:37 UTC, Jonathan M Davis wrote: Is this bug ever going to be fixed?I've filed this issue under https://issues.dlang.org/show_bug.cgi?id=16586 a while ago, seems to have not been picked up yet.
Jun 05 2017