control flow with a functional template ?

Baz via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun May 17 23:13:48 PDT 2015


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 ?


More information about the Digitalmars-d-learn mailing list