www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - interesting semantic possible?

reply "JS" <js.mdnq gmail.com> writes:
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);


using such a notation could allow us to use such a notation could 
make life more interesting... (BTW, I'm not saying this notation 
has to be exactly like this...)

The idea is also to avoid littering the symbol table with symbols 
so "strings without quotes" are passed. e.g., split!Left instead 
of split!"Left" and Left doesn't have to be defined somewhere.

If this notation doesn't work well with template notation then 
some alternate could be used

strip Left or strip#Left..
Jul 21 2013
next sibling parent "JS" <js.mdnq gmail.com> writes:
BTW, I'm not saying D can't do anything like this

enum Dir { Left, Right, Both }

string strip(Dir dir = Dir.Both)(string s)

but one has to call it like

strip!(Dir.Left)(s); (or if using strings, strip!"Left"(s))

Possibly, to make it easier the compiler and localize the enum if 
one is used:

strip!Left(s); // compiler changes to strip!(Dir.Left)(s)
Jul 21 2013
prev sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
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
Jul 21 2013