control flow with a functional template ?

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 18 01:46:35 PDT 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.


More information about the Digitalmars-d-learn mailing list