static try?

bcs bcs at example.com
Tue Nov 1 19:57:29 PDT 2011


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?

> Kenji Hara
>
> 2011/10/31 Mehrdad<wfunction at hotmail.com>:
>> I've written this piece of code a fair number of times:
>>
>>      static if (is(typeof(foo()))) { foo(); }
>>      else { bar(); }
>>
>> When the expression inside the condition (i.e. the call to foo()) gets
>> complicated, you get lots of code duplication and things become harder to
>> read.
>>
>> So I'm thinking, why not just introduce a 'static try'?
>>
>> Something like:
>>
>>     static try
>>     {
>>         foo();
>>     }
>>     catch  // (string ex)  // perhaps let them know what the error is?
>>     {
>>         bar();
>>     }
>>
>> It's a clean and immensely readable improvement IMO, and it doesn't
>> introduce any new keywords or any breaking changes to anything.
>>
>> How's the idea?
>>



More information about the Digitalmars-d mailing list