Deprecate implicit `int` to `bool` conversion for integer literals

Steven Schveighoffer schveiguy at yahoo.com
Tue Nov 14 23:41:39 UTC 2017


On 11/14/17 6:14 PM, Michael V. Franklin wrote:
> On Tuesday, 14 November 2017 at 13:20:22 UTC, Nick Treleaven wrote:
> 
>> An very similar problem exists for int and char overloads:
>>
>> alias foo = (char c) => 1;
>> alias foo = (int i) => 4;
>>
>> enum int e = 7;
>> static assert(foo(e) == 4); // fails
> 
> Wait a minute!  This doesn't appear to be a casting or overload 
> problem.  Can you really overload aliases in D?

In fact, I'm surprised you can alias to an expression like that. Usually 
you need a symbol. It's probably due to how this is lowered.

Indeed, this is a completely different problem:

enum int e = 500;
static assert(foo(e) == 4); // fails to compile (can't call char with 500)

If you define foo as an actual overloaded function set, it works as 
expected.

> 
> I would expect the compiler to throw an error as `foo` is being 
> redefined.  Or for `foo` to be replaced by the most recent assignment in 
> lexical order.  Am I missing something?

In this case, the compiler simply *ignores* the newest definition. It 
should throw an error IMO.

-Steve





More information about the Digitalmars-d mailing list