www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Pseudo member aliases

reply Deokjae Lee <asitdepends gmail.com> writes:
void func(K,V)(in K[V] x) {
}

void main(string[] args) {
	auto x = [1:"1", 2:"2"];
	x.func();
}

Ok, the function func can be called like a member function of a type K[V].
Is there any way to do similar work on alias?
Actually I'd like to define aliases ValueType and KeyType on associative 
arrays and plain arrays, so that they are like member aliases of the 
array types.
If it is possible, code like the following would be compiled using them.

writeln(typeid((string[]).KeyType))); //print "int"
writeln(typeid((string[]).ValueType))); //print "immutable(char)[]"
auto x = [1:"hello", 2:"world"];
writeln(typeid(x.ValueType));//print "immutable(char)[]"
Aug 12 2010
parent bearophile <bearophileHUGS lycos.com> writes:
Deokjae:
 Ok, the function func can be called like a member function of a type K[V].
 Is there any way to do similar work on alias?
 Actually I'd like to define aliases ValueType and KeyType on associative 
 arrays and plain arrays, so that they are like member aliases of the 
 array types.
 If it is possible, code like the following would be compiled using them.
 
 writeln(typeid((string[]).KeyType))); //print "int"
 writeln(typeid((string[]).ValueType))); //print "immutable(char)[]"
 auto x = [1:"hello", 2:"world"];
 writeln(typeid(x.ValueType));//print "immutable(char)[]"
You may create a template that accepts an alias, and contains a static if to tell apart AAs and arrays, and become alias of the key or value type. But then you have to use them with a different syntax, as a normal template. Bye, bearophile
Aug 12 2010