static try?

deadalnix deadalnix at gmail.com
Wed Nov 2 04:53:39 PDT 2011


Le 31/10/2011 23:29, Mehrdad a écrit :
> On 10/31/2011 4:16 AM, deadalnix wrote:
>> Le 31/10/2011 02:21, Mehrdad a écrit :
>>> 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?
>>
>> That sound dangerous. You can get some compile error and not notice it.
> Huh? How is that any worse than is(typeof({ some random code })), which
> we're already doing all over the place?

At least, this if(is(typeof())) isn't executed and isn't supposed to be 
executed.

Here you just created a piece of code that you don't know if it will be 
executed and that will silently fail with no error message.

It remind me INTERCAL and it's styupid comment mecanism (at least, it 
was intentionnaly stupid with INTERCAL) : 
http://en.wikipedia.org/wiki/INTERCAL


More information about the Digitalmars-d mailing list