It's a class! It's a struct! It's ... SuperStruct!
rcorre via Digitalmars-d-announce
digitalmars-d-announce at puremagic.com
Thu Oct 22 18:39:12 PDT 2015
On Thursday, 22 October 2015 at 02:35:34 UTC, rcorre wrote:
>
> Come to think of it, SuperStruct actually sounds pretty similar
> to std.range.chooseAmong (which I just realized exists).
It seems to work quite nicely as an alternative to choose that
works with any types as opposed to just ranges:
auto pick(T...)(size_t index, T values) {
foreach(idx, val ; values)
if (idx == index)
return SuperStruct!T(val);
assert(0, "index not in range of provided values");
}
/// `pick` is useful for something that is a floor wax _and_ a
dessert topping:
unittest {
struct FloorWax { string itIs() { return "a floor wax!";
} }
struct DessertTopping { string itIs() { return "a dessert
topping!"; } }
auto shimmer(bool hungry) {
return pick(hungry, FloorWax(), DessertTopping());
}
assert(shimmer(false).itIs == "a floor wax!");
assert(shimmer(true ).itIs == "a dessert topping!");
}
I can't think of a time I've wanted to do this with something
_other_ than a range, but hey, its there.
More information about the Digitalmars-d-announce
mailing list