1 matches bool, 2 matches long

Ali Çehreli acehreli at yahoo.com
Thu Apr 25 22:49:42 PDT 2013


On 04/25/2013 10:02 PM, Walter Bright wrote:> On 4/25/2013 7:54 PM, 
Kapps wrote:
 >> This is just silly.
 >> Changing enum defaultVal = 1 to defaultVal = 2 should never result in
 >> calling a
 >> different overload.
 >
 > This does:
 >
 > ------------------------
 > import core.stdc.stdio;
 >
 > enum x = 10000;
 > enum y = 40000;
 >
 > int foo(short s) { return 1; }
 > int foo(long s) { return 2; }
 >
 > void main()
 > {
 >      printf("%d\n", foo(x));
 >      printf("%d\n", foo(y));
 > }
 > -------------------------
 >
 > A bool is an integer with the range 0..1

It certainly behaves that way but it isn't an integer type and that's 
why it is unintuitive.

bool is a type with two values: false and true with the following 
conversion rules:

false -> 0
true -> 1
0 value -> false
non-zero value -> true
0 literal -> false

The following are the problematic ones:

1 literal -> true
non-zero and non-one *literal* -> Not a bool!

That last rule is the problem. Since we cannot get rid of the last rule, 
to be consistent, we should make literal 1 match an integer type better 
than bool.

Ali



More information about the Digitalmars-d mailing list