typedef, Typedef
bearophile
bearophileHUGS at lycos.com
Sat Nov 23 12:55:44 PST 2013
This works if you compile with the -d switch:
void main() {
typedef int T;
T[10] a1;
a1[] = 10;
T[] a2 = [1, 2, 3];
T[T] aa = [1:2, 3:4];
}
std.typecons.Typedef should replace the built-in typedef, but
currently you can't use it in those cases:
void main() {
import std.typecons: Typedef;
alias T = Typedef!int;
T[10] a1;
a1[] = T(10); // OK
a1[] = 10; // Error
T[] a2 = [1, 2, 3]; // Error
T[T] aa = [1:2, 3:4]; // Error
}
The problem with "a1[] = 10;":
http://d.puremagic.com/issues/show_bug.cgi?id=6523
But this use case is more important:
T[] a2 = [1, 2, 3];
Workarounds:
T[] a2 = [T(1), T(2), T(3)];
T[T] aa = [T(1):T(2), T(3):T(4)];
T[] a2 = [1, 2, 3].map!T.array;
T[T] aa = zip([1,3].map!T, [2,4].map!T).assocArray;
But both workarounds are bad when you have many items, a long
name for the typedef, or your T is the type of the keys or values
of an associative array.
Can this situation be fixed/improved?
Bye,
bearophile
More information about the Digitalmars-d
mailing list