digitalmars.D.learn - Types soup with enum and bool
- bearophile (12/12) Oct 29 2013 This code is accepted by the D compiler:
- Maxim Fomin (17/29) Oct 29 2013 Probably may be related to even worse issue:
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (5/37) Oct 29 2013 There was a long discussion about that. Walter was happy that bool was a...
- John Colvin (4/51) Oct 29 2013 Hey, just be happy that we're not in IDL, where even numbers
- bearophile (6/15) Oct 29 2013 My problem is mostly with enums. I don't remember Walter
- Maxim Fomin (7/22) Oct 29 2013 Bools being integer types is reason of your problem with enums.
- bearophile (6/7) Oct 29 2013 The reason of that problem of mine with enums is that they
- Maxim Fomin (5/12) Oct 29 2013 It works according to spec: "A named enum member can be
- bearophile (7/11) Oct 29 2013 In C conversion in both directions is allowed (C enums don't have
This code is accepted by the D compiler: enum Foo { A, B, C } void main() { bool[5] bools; auto b = bools[2] != Foo.C; bools[2] = Foo.A; } Who is that likes such kind of code? What are the advantages of accepting such kind of code? I can see the disadvantages and risks. Bye, bearophile
Oct 29 2013
On Tuesday, 29 October 2013 at 12:43:17 UTC, bearophile wrote:This code is accepted by the D compiler: enum Foo { A, B, C } void main() { bool[5] bools; auto b = bools[2] != Foo.C; bools[2] = Foo.A; } Who is that likes such kind of code? What are the advantages of accepting such kind of code? I can see the disadvantages and risks. Bye, bearophileProbably may be related to even worse issue: import std.stdio; void foo(bool b) { writeln("bool"); } void foo(long l) { writeln("long"); } void main() { foo(0); // bool foo(1); // bool foo(2); // long int i = true; foo(i); // long } If reasons for accepting yours and this example are the same, then this is by design (to be more precise, the part which is related to bool types being essentially kind of integer types + VRP + overloading rules).
Oct 29 2013
On 10/29/2013 09:15 AM, Maxim Fomin wrote:On Tuesday, 29 October 2013 at 12:43:17 UTC, bearophile wrote:There was a long discussion about that. Walter was happy that bool was a integer type. Many of us had objections: http://forum.dlang.org/thread/klc5r7$3c4$1 digitalmars.com AliThis code is accepted by the D compiler: enum Foo { A, B, C } void main() { bool[5] bools; auto b = bools[2] != Foo.C; bools[2] = Foo.A; } Who is that likes such kind of code? What are the advantages of accepting such kind of code? I can see the disadvantages and risks. Bye, bearophileProbably may be related to even worse issue: import std.stdio; void foo(bool b) { writeln("bool"); } void foo(long l) { writeln("long"); } void main() { foo(0); // bool foo(1); // bool foo(2); // long int i = true; foo(i); // long } If reasons for accepting yours and this example are the same, then this is by design (to be more precise, the part which is related to bool types being essentially kind of integer types + VRP + overloading rules).
Oct 29 2013
On Tuesday, 29 October 2013 at 17:40:23 UTC, Ali Çehreli wrote:On 10/29/2013 09:15 AM, Maxim Fomin wrote:Hey, just be happy that we're not in IDL, where even numbers evaluate as false, odd numbers evaluate as true and 'not x' evaluates to -(x+1)On Tuesday, 29 October 2013 at 12:43:17 UTC, bearophile wrote:ofThis code is accepted by the D compiler: enum Foo { A, B, C } void main() { bool[5] bools; auto b = bools[2] != Foo.C; bools[2] = Foo.A; } Who is that likes such kind of code? What are the advantagesrisks.accepting such kind of code? I can see the disadvantages andthen thisBye, bearophileProbably may be related to even worse issue: import std.stdio; void foo(bool b) { writeln("bool"); } void foo(long l) { writeln("long"); } void main() { foo(0); // bool foo(1); // bool foo(2); // long int i = true; foo(i); // long } If reasons for accepting yours and this example are the same,is by design (to be more precise, the part which is relatedto booltypes being essentially kind of integer types + VRP +overloading rules). There was a long discussion about that. Walter was happy that bool was a integer type. Many of us had objections: http://forum.dlang.org/thread/klc5r7$3c4$1 digitalmars.com Ali
Oct 29 2013
Ali Çehreli:My problem is mostly with enums. I don't remember Walter explaining the rationale of the D enum conversion design. I prefer the C++11 enums. Bye, bearophile... There was a long discussion about that. Walter was happy that bool was a integer type. Many of us had objections:enum Foo { A, B, C } void main() { bool[5] bools; auto b = bools[2] != Foo.C; bools[2] = Foo.A; }
Oct 29 2013
On Tuesday, 29 October 2013 at 18:23:03 UTC, bearophile wrote:Ali Çehreli:Bools being integer types is reason of your problem with enums. Relevant lines from your code are translated to: bool b = 0 != 2; bools[2] = 0; taking into account that 'bool' is integer type having capacity to represent values 0 and 1.My problem is mostly with enums. I don't remember Walter explaining the rationale of the D enum conversion design. I prefer the C++11 enums. Bye, bearophile... There was a long discussion about that. Walter was happy that bool was a integer type. Many of us had objections:enum Foo { A, B, C } void main() { bool[5] bools; auto b = bools[2] != Foo.C; bools[2] = Foo.A; }
Oct 29 2013
Maxim Fomin:Bools being integer types is reason of your problem with enums.The reason of that problem of mine with enums is that they convert implicitly to integers. And I still don't know the original rationale of Walter of this design mistake. Bye, bearophile
Oct 29 2013
On Tuesday, 29 October 2013 at 21:24:35 UTC, bearophile wrote:Maxim Fomin:It works according to spec: "A named enum member can be implicitly cast to its EnumBaseType, but EnumBaseType types cannot be implicitly cast to an enum type. " In this respect D enums behave like C enums.Bools being integer types is reason of your problem with enums.The reason of that problem of mine with enums is that they convert implicitly to integers. And I still don't know the original rationale of Walter of this design mistake. Bye, bearophile
Oct 29 2013
Maxim Fomin:It works according to spec: "A named enum member can be implicitly cast to its EnumBaseType, but EnumBaseType types cannot be implicitly cast to an enum type. " In this respect D enums behave like C enums.In C conversion in both directions is allowed (C enums don't have a EnumBaseType, well, it's int). But why are D specs prescribing D enums to be implicitly castable to its EnumBaseType? Bye, bearophile
Oct 29 2013