Implicit enum conversions are a stupid PITA

Ellery Newcomer ellery-newcomer at utulsa.edu
Thu Mar 25 14:25:31 PDT 2010


On 03/25/2010 02:00 PM, Walter Bright wrote:
>>
>> ------- 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.

I know. It was my understanding that this entire thread was a complaint 
of this behavior.
>
> This allows the user complete control over which functions get
> overloaded when they come from diverse, and possibly completely
> independent, imports.
>

It's a very explicit form of control.

Suppose I have 10 000 enums (I don't have quite that many, but there are 
a lot), each of which is in a different module because each is logically 
connected to something else. Because enums in D are too spartan for my 
tastes, I define a mixin to generate an enum, along with several helper 
functions, like name, fromInt, toInt, etc. (Note that a way to enumerate 
an enum's values would obviate all this, and __traits isn't an option in D1)

It's my understanding that enums are strongly enough typed that calling 
such a helper function with an enum (or anything else) not defined for 
it would generate an error. So there shouldn't be any way to hijack it.

But if I have some enum A defined in my current module and I also want 
to use enums modl2.B, modl3.C, etc, then I have to manually add all those

alias modl2.name name;

<poke> or else write an IDE tool that automatically generates them for 
me </poke>

I guess what I'm trying to say is it doesn't make sense that I can 
implicitly import FooA, an external symbol, but not bar(FooA), an 
external symbol defined on an external symbol which cannot be implicitly 
converted to a local symbol.

>
>
>> ------- 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