Enums - probably an old subject

monarch_dodra monarchdodra at gmail.com
Thu Nov 21 23:07:42 PST 2013


On Friday, 22 November 2013 at 00:50:25 UTC, John J wrote:
> On 11/21/2013 02:20 PM, monarch_dodra wrote:
>>
>> When you import from a module, you only need to specify the 
>> module name
>> if there is ambiguity. So for example:
>>
>> //----
>> import std.array;
>>
>> void main()
>> {
>>     split("hello"); //OK!
>> }
>> //----
>> import std.array;
>> import std.algorithm;
>>
>> void main()
>> {
>>     split("hello"); //Wait... did you want 
>> std.algorithm.split, or
>> std.array.split?
>>     std.array.split("hello"); //OK! That's clearer now.
>> }
>> //----
>>
>> What Andrei is saying is that an enum *could* work the same 
>> way:
>>
>> enum RedBlack //For red black trees
>> {
>>     Red,
>>     Black,
>> }
>> enum BlackWhite //For Checker boards
>> {
>>     Black,
>>     White,
>> }
>>
>> void main()
>> {
>>     writeln(Red); //OK! No problem!
>>     writeln(Black); //I'm sorry... that's ambiguous :/
>>     writeln(RedBlack.Black); //OK! That's clearer now!
>> }
>>
>> Whether or not it *should*, I don't know. "Why not?" I like to 
>> say :)
>
> If both these enums are coming from a module and I also have a 
> RedBlack enum in my main program that has Black in it, then I 
> guess it would be
>
> modulename.RedBlack.Black
>
> to call the one from the module.
>
> Does it make sense?
> I should be really in D.learn but asked that here in context ;)

If the enum's name is RedBlack, and you want the Black value, 
than yes., you'd need to "double disambiguate".

If you wanted Red though, then:
modulename.Red
Should be enough to un-ambiguously refer to the correct enum 
value.


More information about the Digitalmars-d mailing list