digitalmars.D.learn - control flow with a functional template ?
- Baz (37/37) May 17 2015 who's never had to do this:
- John Colvin (4/42) May 18 2015 Take a look at lazy function arguments if you really want this.
- John Colvin (3/54) May 18 2015 Wait no, actually you do need mixins, I wasn't thinking. Unless
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
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
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:Wait no, actually you do need mixins, I wasn't thinking. Unless you make the if statement a function call / template...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








"John Colvin" <john.loughran.colvin gmail.com>