| 1 |
/* |
| 2 |
PERMUTE_ARGS: |
| 3 |
*/ |
| 4 |
|
| 5 |
version (with_phobos) import std.conv : text; |
| 6 |
|
| 7 |
struct Tuple(T...) |
| 8 |
{ |
| 9 |
T expand; |
| 10 |
alias expand this; |
| 11 |
} |
| 12 |
|
| 13 |
auto tuple(T...)(T t) |
| 14 |
{ |
| 15 |
return Tuple!T(t); |
| 16 |
} |
| 17 |
|
| 18 |
void fun0(U, T...)(U gold, int b_gold, T a, int b) |
| 19 |
{ |
| 20 |
assert(tuple(a) == gold); |
| 21 |
assert(b == b_gold); |
| 22 |
} |
| 23 |
|
| 24 |
void fun(U, T...)(U gold, T a, int b = 1) |
| 25 |
{ |
| 26 |
assert(tuple(a) == gold); |
| 27 |
assert(b == 1); |
| 28 |
} |
| 29 |
|
| 30 |
void fun2(U, V, T...)(U gold, V gold2, T a, string file = __FILE__, int line = __LINE__) |
| 31 |
{ |
| 32 |
assert(tuple(a) == gold); |
| 33 |
assert(tuple(file, line) == gold2); |
| 34 |
} |
| 35 |
|
| 36 |
void fun3(int[] gold, int[] a...) |
| 37 |
{ |
| 38 |
assert(gold == a); |
| 39 |
} |
| 40 |
|
| 41 |
void fun4(T...)(size_t length_gold, int b_gold, T a, int b = 1) |
| 42 |
{ |
| 43 |
assert(T.length == length_gold); |
| 44 |
assert(b == b_gold); |
| 45 |
} |
| 46 |
|
| 47 |
// Example in changelog |
| 48 |
string log(T...)(T a, string file = __FILE__, int line = __LINE__) |
| 49 |
{ |
| 50 |
return text(file, ":", line, " ", a); |
| 51 |
} |
| 52 |
|
| 53 |
void fun_constraint(T...)(T a, string b = "bar") if (T.length == 1) |
| 54 |
{ |
| 55 |
} |
| 56 |
|
| 57 |
/+ |
| 58 |
NOTE: this is disallowed by the parser: |
| 59 |
void fun5(int[] gold, int[] a ..., int b = 1) |
| 60 |
{ |
| 61 |
assert(gold==a); |
| 62 |
assert(b==1); |
| 63 |
} |
| 64 |
+/ |
| 65 |
|
| 66 |
void main() |
| 67 |
{ |
| 68 |
fun0(tuple(10), 7, 10, 7); |
| 69 |
|
| 70 |
fun(tuple()); |
| 71 |
fun(tuple(10), 10); |
| 72 |
fun(tuple(10, 11), 10, 11); |
| 73 |
|
| 74 |
fun2(tuple(10), tuple(__FILE__, __LINE__), 10); |
| 75 |
|
| 76 |
fun3([1, 2, 3], 1, 2, 3); |
| 77 |
|
| 78 |
fun_constraint(1); |
| 79 |
assert(!__traits(compiles, fun_constraint(1, "baz"))); |
| 80 |
|
| 81 |
version (with_phobos) |
| 82 |
assert(log(10, "abc") == text(__FILE__, ":", __LINE__, " 10abc")); |
| 83 |
|
| 84 |
// IFTI: `b` is always default-set |
| 85 |
fun4(0, 1); |
| 86 |
fun4(1, 1, 10); |
| 87 |
fun4(2, 1, 10, 11); |
| 88 |
|
| 89 |
// with explicit instantiation, and default-set `b` |
| 90 |
fun4!int(1, 1, 10); |
| 91 |
fun4!(int, int)(2, 1, 10, 11); |
| 92 |
|
| 93 |
// with explicit instantiation, and over-ridden `b` |
| 94 |
fun4!int(1, 100, 10, 100); |
| 95 |
fun4!(int, int)(2, 100, 10, 11, 100); |
| 96 |
|
| 97 |
// fun5([1,2,3], 1,2,3); |
| 98 |
} |