static try?

Don nospam at nospam.com
Wed Nov 2 00:24:46 PDT 2011


On 02.11.2011 03:57, bcs wrote:
> On 11/01/2011 10:04 AM, kenji hara wrote:
>> --1;
>>
>> It you want to reduce duplication of long code, you can use string mixin.
>> enum code = q{ ...long code... };
>> static if (is(typeof({ mixin(code); })))
>> {
>> mixin(code);
>> }
>> else
>> {
>> ...
>> }
>>
>
> Please no! I've thought for years that string mixins are seriously
> overused in D. IMnsHO they should only be used as a method of last
> resort. For every case I've seen where there is a alternative to a
> string mixin, the alternative was cleaner.
>
> If people don't like the static try/catch, how about: static try/else?

Probably worth noting that in the latest release, you can instantiate a 
template from inside an is(typeof()), and if it fails to compile, the 
template instantiation will be discarded. (Prior to 2.056, if the 
template instantiation failed, compilation would fail).

This means you can now factor out "does it compile?" tests into a 
template, to reduce code duplication.


More information about the Digitalmars-d mailing list