proposal: tthis for templates, or private aliases
Bill Baxter
dnewsgroup at billbaxter.com
Thu Dec 14 16:31:51 PST 2006
It gets tiring to type stuff like this:
template convertable_type_in_tuple(T, S...) {
static if( is(S[0] : T) ) {
const bool convertable_type_in_tuple =
convertable_type_in_tuple!(T, S[1..$]);
} else static if(S.length == 0) {
const bool convertable_type_in_tuple = true;
} else {
const bool convertable_type_in_tuple = false;
}
}
This is the general pattern for recursive tuple templates. You
generally end up repeating the name of the template at least four times
inside the body of the template.
Then you decide 'convertable_type_in_tuple' wasn't a good name and you
have to be sure to correct it everywhere or you'll get compiler errors
that only show up when that case is actually exercised.
So proposal option one is to give templates declared with the "template
identifier(args)..." syntax to use a special kind of 'this', call it a
template this or 'tthis', for lack of a better idea.
template convertable_type_in_tuple(T, S...) {
static if( is(S[0] : T) ) {
const bool tthis = tthis!(T, S[1..$]);
} else static if(S.length == 0) {
const bool tthis = true;
} else {
const bool tthis = false;
}
}
Option 2 is to allow some sort of private alias in a template that
doesn't interfere with the inner-name-matches-outer-name magic.
template convertable_type_in_tuple(T, S...) {
private alias convertable_type_in_tuple tthis;
static if( is(S[0] : T) ) {
const bool tthis = tthis!(T, S[1..$]);
} else static if(S.length == 0) {
const bool tthis = true;
} else {
const bool tthis = false;
}
}
Then at least you only have to type the name twice.
Option 2 would also be useful for function templates that use
non-trivial derived types:
template function(T) {
private alias typeof(T.init.member) RetT;
RetT function(T val) {
return val.member;
}
}
--bb
More information about the Digitalmars-d
mailing list