Skip to content

Commit

Permalink
Merge pull request #1235 from AndrejMitrovic/Fix9839
Browse files Browse the repository at this point in the history
Issue 9839 - Select should be able to select symbols.
  • Loading branch information
9rnsr committed Mar 31, 2013
2 parents 3dc910f + 320b73a commit 39dd56d
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions std/traits.d
Expand Up @@ -5845,24 +5845,28 @@ unittest
// XXX Select & select should go to another module. (functional or algorithm?)

/**
Aliases itself to $(D T) if the boolean $(D condition) is $(D true)
and to $(D F) otherwise.
Example:
----
alias Select!(size_t.sizeof == 4, int, long) Int;
----
Aliases itself to $(D T[0]) if the boolean $(D condition) is $(D true)
and to $(D T[1]) otherwise.
*/
template Select(bool condition, T, F)
template Select(bool condition, T...) if (T.length == 2)
{
static if (condition) alias T Select;
else alias F Select;
alias Select = T[!condition];
}

///
unittest
{
// can select types
static assert(is(Select!(true, int, long) == int));
static assert(is(Select!(false, int, long) == long));

// can select symbols
int a = 1;
int b = 2;
alias selA = Select!(true, a, b);
alias selB = Select!(false, a, b);
assert(selA == 1);
assert(selB == 2);
}

/**
Expand Down

0 comments on commit 39dd56d

Please sign in to comment.