foreach over split string

JS js.mdnq at gmail.com
Thu Jul 18 03:07:58 PDT 2013


Since you do such a good job a explaining things, would you mind 
informing me what the difference between alias T, T and string T 
used as template parameters are?

template A!(T) { }
template A!(alias T) { }
template A!(string T) { }


The string version is suppose to be a specialization of A that is 
called when A is passed a string? e.g., A!("...") calls the 
string version? (this is my assumption but I seem to run into 
problems where sometimes other versions are called)

The alias T version seems to accept a symbol alias(a sort of 
redirection/substitution is made but no actual evaluation is made 
on the symbol).

The T version accepts types and compile type 
constants/literals(like a string).


This is what I understand them to do... but always get into 
trouble where something wants an alias while another thing wants 
a type.

moduleName complains a lot because sometimes I pass it a built in 
type like double , sometimes a string(by accident but I want it 
to return the string itself), and sometimes I pass a user type.

I have to rectify all 3 usages into one common template so I wrap 
moduleName into, say ModuleName.

template ModuleName(T) { return (isBasicType!T) ? "" : 
moduleName!(T); }
template ModuleName(string s) { return s; }

but sometimes, it seems the T version is called when I pass it a 
string.

Error: template instance moduleName!(string) does not match 
template declaration moduleName(alias T)

when I use

template ModuleName(T)
{
     static if (isValueType!T)
         enum ModuleName = "";
     else static if (isString!T)
         enum ModuleName = T;
     else
         enum ModuleName = StripStr!(std.traits.moduleName!T, `"`, 
" ");
}

template ModuleName(string s) { enum ModuleName = s; }

StripStr is just a template that strips the ends of a string. 
isValueType is a template wrapper to test T for a value 
type(scalar, void, string... *all* types that do not have/need 
module resolution)

The error suggests that I'm passing a string into the 
std.traits.moduleName call but this shouldn't happen?



More information about the Digitalmars-d-learn mailing list