Skip to content

Commit e8ef1b8

Browse files
authored
[spec] Add __traits(parameters) example (#3795)
1 parent f0166f0 commit e8ef1b8

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

spec/traits.dd

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,12 +1076,26 @@ static assert(__traits(getParameterStorageClasses, foo(p, a, b, c), 3)[0] == "la
10761076
$(H3 $(GNAME parameters))
10771077

10781078
$(P May only be used inside a function. Takes no arguments, and returns
1079-
a sequence of the enclosing function's parameters.)
1080-
1081-
$(P If the function is nested, the parameters returned are those of the
1082-
inner function, not the outer one.)
1079+
an $(DDSUBLINK spec/template, lvalue-sequences, lvalue sequence) of the
1080+
enclosing function's parameters.)
10831081

1082+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
10841083
---
1084+
alias AliasSeq(A...) = A;
1085+
1086+
void f(int n, char c)
1087+
{
1088+
alias PS = __traits(parameters);
1089+
PS[0]++; // increment n
1090+
static assert(is(typeof(PS) == AliasSeq!(int, char)));
1091+
1092+
// output parameter names
1093+
static foreach (i, p; PS)
1094+
{
1095+
pragma(msg, __traits(identifier, p));
1096+
}
1097+
}
1098+
10851099
int add(int x, int y)
10861100
{
10871101
return x + y;
@@ -1093,7 +1107,13 @@ $(H3 $(GNAME parameters))
10931107
// equivalent to;
10941108
//return add(x, y);
10951109
}
1110+
---
1111+
)
1112+
1113+
$(P If the function is nested, the parameters returned are those of the
1114+
inner function, not the outer one.)
10961115

1116+
---
10971117
int nestedExample(int x)
10981118
{
10991119
// outer function's parameters
@@ -1108,7 +1128,8 @@ $(H3 $(GNAME parameters))
11081128

11091129
return add(x, x);
11101130
}
1111-
1131+
---
1132+
---
11121133
class C
11131134
{
11141135
int opApply(int delegate(size_t, C) dg)

0 commit comments

Comments
 (0)