digitalmars.D.learn - Pseudo member aliases
- Deokjae Lee (16/16) Aug 12 2010 void func(K,V)(in K[V] x) {
- bearophile (4/15) Aug 12 2010 You may create a template that accepts an alias, and contains a static i...
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
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