Select value from list, indexed by a type
Johan Engelen via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Jul 28 14:12:11 PDT 2015
Hi all,
I am wondering if there is any Phobos functionality for
indexing into a list using a type. What I mean is something like:
assert( somethingie!(float, float, double, real)(1, 22, 333) ==
1 );
assert( somethingie!(double, float, double, real)(1, 22, 333) ==
22 );
assert( somethingie!(real, float, double, real)(1, 22, 333) ==
333 );
It would help with unittest definitions, such as:
foreach (F; TypeTuple!(float, double, real))
{
Tuple!(F, int)[] vals = // x, ilogb(x)
[
tuple( 2.0001 , 1 ),
tuple( 1.9999 , 0 ),
// ...
tuple(-F.min_normal, somethingie!(F, float, double,
real)(-126, -1022, -16382),
];
foreach(elem; vals)
{
assert(ilogb(elem[0]) == elem[1]);
}
}
Thanks!
(on IRC, I was advised to move the special case of min_normal
outside of the loop. Perhaps more people like that better, but
still I am interested in knowing if my "somethingie" exists, or
whether you think it is useful too. (what if there are 10 such
special cases?))
More information about the Digitalmars-d-learn
mailing list