A code example that shows why I don't like warnings

Daniel N no at public.email
Sun Oct 20 11:00:44 UTC 2019


On Saturday, 19 October 2019 at 16:14:20 UTC, Alexandru Ermicioi 
wrote:
> On Friday, 18 October 2019 at 18:22:33 UTC, Adam D. Ruppe wrote:
>> On Friday, 18 October 2019 at 18:19:55 UTC, Alexandru Ermicioi 
>> wrote:
>>> You can define a bool enum in the body of static if (inside 
>>> foreach)  and then wrap default return statement with static 
>>> if that checks if that enum was defined. If it wasn't then 
>>> return "default".
>>
>> oh that's not horrible and it works too. But still I don't 
>> love it :)
>>
>> BTW as of today my whole lib will compile with warnings 
>> including when exercising the more complicated templates!
>
> Yeah it is not ideal. This trick can also shield you in case if 
> multiple elements from static foreach matches inner static if, 
> just by adding a check to it if this enum was defined earlier. 
> Also beware of code blocks for foreach or inner static if, 
> since it will limit enum declaration to that block and wont be 
> available to static if defined outside of foreach (the one for 
> default return).

template defaultFormat(alias method)
{
         static foreach(attr; __traits(getAttributes, method))
                 static if(is(typeof(attr) == string))
                         enum defaultFormat = attr;

         static if(is(typeof(defaultFormat) == void))
	        enum defaultFormat = "default";
}



More information about the Digitalmars-d mailing list