interesting semantic possible?

bearophile bearophileHUGS at lycos.com
Sun Jul 21 18:19:51 PDT 2013


JS:

> Say I have a template function where there are variations, it 
> would be nice to be able to specify those variables like
>
> strip!Left(s), strip!Right(s), strip(s), etc...
>
> instead of ahving to do stripLeft, stripRight, and strip.
>
> But the goal isn't just to add an extra ! to type but that it 
> reduces code duplication.
>
> strip left and strip right are very similar and in a stripping 
> algorithm most of the code is identical except for the 
> direction the loop... instead of copying and pasting an 
> appropriately placed static if could simplify things greatly.
>
> e.g.,
>
> string strip(string dir = {"Left", "Right", default: 
> "Both"})(string s);

You are discussing about two different problems: one is the 
duplication of code in strip, and the other in having a syntax 
for "inlined" enums.

The first problem is easy to solve, just write one strip function 
with a template argument to tell apart its various versions, and 
then add:

alias stripLeft = strip!(Dir.Left);
alias stripRight = strip!(Dir.Right);
...

So each version only requires one more line of code, this is 
acceptable.

Regarding the inlined enum, a similar feature was requested 
several times, maybe in some form even in Bugzilla, but I don't 
remember Walter ever commenting on it.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list