Types soup with enum and bool
Maxim Fomin
maxim at maxim-fomin.ru
Tue Oct 29 09:15:02 PDT 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,
> 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).
More information about the Digitalmars-d-learn
mailing list