pick

Wrap one of two values in a SuperStruct that encompasses both types.

This can be used to return a value from one of several different types. Similar to std.range.chooseAmong, but for a broader range of types.

pick
(
T1
T2
)
(,
T1 a
,
T2 b
)

Parameters

condition bool

which value to choose: a if true, b otherwise

a T1

the "true" value

b T2

the "false" value

Return Value

Type: auto

A SuperStruct!(T1,T2) wrapping the selected value.

Examples

struct Square { int size; int area() { return size * size; } }
struct Rect   { int w ,h; int area() { return w * h; } }

auto shape(int w, int h) {
  return pick(w == h, Square(w), Rect(w,h));
}

assert(shape(4,5).area == 20);
assert(shape(3,3).area == 9);

pick is useful for something that is a floor wax _and_ a dessert topping:

struct FloorWax       { string itIs() { return "a floor wax!";       } }
struct DessertTopping { string itIs() { return "a dessert topping!"; } }

auto shimmer(bool hungry) {
  return pick(hungry, DessertTopping(), FloorWax());
}

assert(shimmer(false).itIs == "a floor wax!");
assert(shimmer(true ).itIs == "a dessert topping!");

Meta