Skip to content

Instantly share code, notes, and snippets.

@CyberShadow
Created March 16, 2013 17:54
Show Gist options
  • Save CyberShadow/5177509 to your computer and use it in GitHub Desktop.
Save CyberShadow/5177509 to your computer and use it in GitHub Desktop.
template Tuple(T...) { alias T Tuple; }
template Select(bool b, T, U) { static if (b) alias T Select; else alias U Select; }
void main()
{
static bool isEq (T1, T2)(T1 s1, T2 s2) { return s1 == s2; }
static bool isNeq(T1, T2)(T1 s1, T2 s2) { return s1 != s2; }
foreach (b1; Tuple!(false, true))
foreach (b2; Tuple!(false, true))
{
assert( isEq (cast(Select!(b1, string, char[1]))"a", cast(Select!(b2, string, char[1]))"a" ));
assert(!isNeq(cast(Select!(b1, string, char[1]))"a", cast(Select!(b2, string, char[1]))"a" ));
assert(!isEq (cast(Select!(b1, string, char[1]))"a", cast(Select!(b2, string, char[1]))"b" ));
assert( isNeq(cast(Select!(b1, string, char[1]))"a", cast(Select!(b2, string, char[1]))"b" ));
assert(!isEq (cast(Select!(b1, string, char[1]))"a", cast(Select!(b2, string, char[2]))"aa"));
assert( isNeq(cast(Select!(b1, string, char[1]))"a", cast(Select!(b2, string, char[2]))"aa"));
// Note: order of evaluation was not followed before this patch
// (thus, the test below will fail without the patch).
// Although the specification mentions that as implementation-defined behavior,
// I understand that this isn't by design, but rather an inconvenient aspect of DMD
// that has been moved to the specification.
int order = 0;
auto getS1()() { assert(order==0); order++; return cast(Select!(b1, string, char[1]))"a"; }
auto getS2()() { assert(order==1); order++; return cast(Select!(b2, string, char[1]))"a"; }
bool result = getS1() == getS2();
assert(result);
assert(order == 2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment