August 12, 2010 Pseudo member aliases | ||||
---|---|---|---|---|
| ||||
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)[]" |
August 12, 2010 Re: Pseudo member aliases | ||||
---|---|---|---|---|
| ||||
Posted in reply to Deokjae Lee | 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
|
Copyright © 1999-2021 by the D Language Foundation