Jump to page: 1 2
Thread overview
static try?
Oct 31, 2011
Mehrdad
Oct 31, 2011
deadalnix
Oct 31, 2011
Mehrdad
Nov 02, 2011
deadalnix
Nov 01, 2011
Robert Jacques
Nov 01, 2011
dennis luehring
Nov 01, 2011
Vladimir Panteleev
Nov 01, 2011
dennis luehring
Nov 01, 2011
kenji hara
Nov 02, 2011
bcs
Nov 02, 2011
Don
Nov 02, 2011
deadalnix
Nov 03, 2011
bcs
Nov 02, 2011
Don
Nov 02, 2011
Mehrdad
Nov 02, 2011
Don
October 31, 2011
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?
October 31, 2011
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.
October 31, 2011
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?
November 01, 2011
On Sun, 30 Oct 2011 21:21:58 -0400, Mehrdad <wfunction@hotmail.com> wrote:

> 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?
>

Vote++
November 01, 2011
Am 31.10.2011 02:21, schrieb Mehrdad:
> 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?

1. sorry but look like static exception based flow control - because of the "try/catch" - and that is evil

2.

static try
{
}

introduces an compiletime scope - what would the following code mean?

static try
{
  int x = 10;
  foo();
  int y = 20;
}
catch
{
  bar();
}


November 01, 2011
On Tue, 01 Nov 2011 18:33:56 +0200, dennis luehring <dl.soluz@gmx.net> wrote:

> introduces an compiletime scope - what would the following code mean?

"debug", "version", "static if" don't create scopes.

-- 
Best regards,
 Vladimir                            mailto:vladimir@thecybershadow.net
November 01, 2011
--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
{
  ...
}

Kenji Hara

2011/10/31 Mehrdad <wfunction@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?
>
November 01, 2011
Am 01.11.2011 17:46, schrieb Vladimir Panteleev:
> On Tue, 01 Nov 2011 18:33:56 +0200, dennis luehring<dl.soluz@gmx.net>
> wrote:
>
>>  introduces an compiletime scope - what would the following code mean?
>
> "debug", "version", "static if" don't create scopes.
>

i know - wrong name for the block between the try{...}

static try
{
------------------
   foo1();
   foo2();
   foo3();
------------------
}
catch
{
   bar();
}

im missing a description of what should happen if any of the foos fails to compile? is then the catch part used in compilation?

the idea is too near to the syntax of the exceptionhandling and have total different semantic... is that good?
November 02, 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@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?
>>

November 02, 2011
On 31.10.2011 02:21, Mehrdad wrote:
> 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.

IMHO the only problem is the is(typeof()) syntax.
Otherwise I don't see how this has any more code duplication than:

if( foo(lots_of_parameters) )
    foo(lots_of_parameters);
else bar();
« First   ‹ Prev
1 2