On Sunday, 15 March 2015 at 22:21:04 UTC, ketmar wrote:
On Sun, 15 Mar 2015 21:59:16 +0000, Charles Cooper wrote:
C++14 has:
template<class T, class... Types> constexpr T&
get(tuple<Types...>& t);
Which allows you to get a member of the tuple struct by type.
Is there
an idiomatic / library way to do this in D? Preferably by
indexing.
why indexing? functional programming is fun!
import std.typecons : tuple;
template GetByType(Type, Tuple...) {
import std.typecons : isTuple;
static if (Tuple.length == 0)
static assert(false, Type.stringof~" not found");
else static if (Tuple.length == 1 &&
isTuple!(typeof(Tuple[0])))
enum GetByType = GetByType!(Type, Tuple[0].expand);
else static if (is(typeof(Tuple[0]) == Type))
enum GetByType = Tuple[0];
else
enum GetByType = GetByType!(Type, Tuple[1..$]);
}
static assert(2.5 == GetByType!(double, 1, 2.5));
static assert(2.5 == GetByType!(double, 1, 2.5, 3.1));
//static assert(2.5 == GetByType!(char, 1, 2.5, 3.1));
static assert(2.5 == GetByType!(double, std.typecons.tuple(1,
2.5)));
static assert(2.5 == GetByType!(double,
std.typecons.tuple(1,2.5,3.1)));
Seems like a useful feature(useful enough to get past the C++
standards committee,) consider submitting a phobos PR?