Implicit enum conversions are a stupid PITA

Walter Bright newshound1 at digitalmars.com
Thu Mar 25 12:00:31 PDT 2010


Ellery Newcomer wrote:
> On 03/24/2010 11:40 PM, Walter Bright wrote:
>> Nick Sabalausky wrote:
>>>> In D1, is there any reason I should be getting an error on this?:
>>>>
>>>> // module A:
>>>> enum FooA { fooA };
>>>> void bar(FooA x) {}
>>>>
>>>> // module B:
>>>> import A;
>>>> enum FooB { fooB };
>>>> void bar(FooB x) {}
>>>>
>>>> bar(FooB.fooB); // Error: A.bar conflicts with B.bar (WTF?)
>>
>> ------- a.d -------------------
>> enum FooA { fooA };
>> void bar(FooA x) {}
>> ------- test.d ----------------
>> import a;
>> enum FooB { fooB };
>> void bar(FooB x) {}
>>
>> void test()
>> {
>> bar(FooB.fooB);
>> }
>> ------------------------------
>>
>> with:
>>
>> dmd test
>>
>> I do not get an error with either D1 or D2. So, if you are getting an
>> error, how is your code different?
> 
> ------- a.d -------------------
> enum FooA { fooA };
> void bar(FooA x) {}
> ------- test.d ----------------
> import a;
> mixin(`
> enum FooB { fooB };
> void bar(FooB x) {}
> `);
> 
> void test()
> {
>     bar(FooA.fooA); //error
>     bar(FooB.fooB);
> }
> ------------------------------


This error is quite correct (and happens with or without the mixin). The idea is 
if you define a function in a local scope, it *completely hides* functions with 
the same name in imported scopes. In order to overload local functions with 
imported ones, an alias is necessary to bring the imported functions into the 
same scope.

This allows the user complete control over which functions get overloaded when 
they come from diverse, and possibly completely independent, imports.



> ------- a.d -------------------
> enum FooA { fooA };
> void bar(FooA x) {}
> ------- test.d ----------------
> import a;
> alias a.bar bar;
> mixin(`
> enum FooB { fooB };
> void bar(FooB x) {}
> `);
> 
> void test()
> {
>     bar(FooA.fooA);
>     bar(FooB.fooB); //error
> }
> ------------------------------

I'm not sure why this error is happening, it definitely has something to do with 
the mixin. Let me look into it some more.



More information about the Digitalmars-d mailing list