www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - control flow with a functional template ?

reply "Baz" <bb.temp gmx.com> writes:
who's never had to do this:

---
if (comparison)
{
      statement;
      break;
}
---

ans then thought it's a pity to open/closes the braces just for a
simple statement. Would it be possible to have a template to
simplify this to:

---
if (comparison)
      Break!(expression);
---

or even at the language level:

---
if (comparison)
      break(expression);
if (comparison)
      continue(expression);
---

so far it looks like it's only possible using a string mixin,
which is a quite unelegant solution (because for example you 
loose the IDE completion while writting the statement):

---
auto Break(string statement)
{
     return format("{%s;break;}", statement);
}
// unelegant but works...
if (condition)
      mixin("myVar = 8".Break);

if (condition)
      mixin(q{myVar = 8}.Break);
---

Any other solution ?
May 17 2015
parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Monday, 18 May 2015 at 06:13:50 UTC, Baz wrote:
 who's never had to do this:

 ---
 if (comparison)
 {
      statement;
      break;
 }
 ---

 ans then thought it's a pity to open/closes the braces just for 
 a
 simple statement. Would it be possible to have a template to
 simplify this to:

 ---
 if (comparison)
      Break!(expression);
 ---

 or even at the language level:

 ---
 if (comparison)
      break(expression);
 if (comparison)
      continue(expression);
 ---

 so far it looks like it's only possible using a string mixin,
 which is a quite unelegant solution (because for example you 
 loose the IDE completion while writting the statement):

 ---
 auto Break(string statement)
 {
     return format("{%s;break;}", statement);
 }
 // unelegant but works...
 if (condition)
      mixin("myVar = 8".Break);

 if (condition)
      mixin(q{myVar = 8}.Break);
 ---

 Any other solution ?
Take a look at lazy function arguments if you really want this. Personally I think you'll end up saving a small handful of keystrokes (if any) at the expense of clarity.
May 18 2015
parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Monday, 18 May 2015 at 08:46:36 UTC, John Colvin wrote:
 On Monday, 18 May 2015 at 06:13:50 UTC, Baz wrote:
 who's never had to do this:

 ---
 if (comparison)
 {
     statement;
     break;
 }
 ---

 ans then thought it's a pity to open/closes the braces just 
 for a
 simple statement. Would it be possible to have a template to
 simplify this to:

 ---
 if (comparison)
     Break!(expression);
 ---

 or even at the language level:

 ---
 if (comparison)
     break(expression);
 if (comparison)
     continue(expression);
 ---

 so far it looks like it's only possible using a string mixin,
 which is a quite unelegant solution (because for example you 
 loose the IDE completion while writting the statement):

 ---
 auto Break(string statement)
 {
    return format("{%s;break;}", statement);
 }
 // unelegant but works...
 if (condition)
     mixin("myVar = 8".Break);

 if (condition)
     mixin(q{myVar = 8}.Break);
 ---

 Any other solution ?
Take a look at lazy function arguments if you really want this. Personally I think you'll end up saving a small handful of keystrokes (if any) at the expense of clarity.
Wait no, actually you do need mixins, I wasn't thinking. Unless you make the if statement a function call / template...
May 18 2015