control flow with a functional template ?
John Colvin via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon May 18 01:48:38 PDT 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:
>> 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...
More information about the Digitalmars-d-learn
mailing list