Types soup with enum and bool

Ali Çehreli acehreli at yahoo.com
Tue Oct 29 10:40:22 PDT 2013


On 10/29/2013 09:15 AM, Maxim Fomin wrote:

 > 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,
 >> bearophile
 >
 > Probably 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).

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



More information about the Digitalmars-d-learn mailing list