Skip to content

Commit df588ad

Browse files
authored
Merge pull request #3251 from pbackus/traits-parameters-spec
Add __traits(parameters) to language spec
2 parents 13f0f1c + 08bce0e commit df588ad

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

spec/traits.dd

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ $(GNAME TraitsKeyword):
8080
$(RELATIVE_LINK2 compiles, $(D compiles))
8181
$(RELATIVE_LINK2 toType, $(D toType))
8282
$(RELATIVE_LINK2 initSymbol, $(D initSymbol))
83+
$(RELATIVE_LINK2 parameters, $(D parameters))
8384

8485
$(GNAME TraitsArguments):
8586
$(GLINK TraitsArgument)
@@ -1772,6 +1773,71 @@ $(H3 $(GNAME initSymbol))
17721773
}
17731774
---
17741775

1776+
$(H3 $(GNAME parameters))
1777+
1778+
$(P May only be used inside a function. Takes no arguments, and returns
1779+
a sequence of the enclosing function's parameters.)
1780+
1781+
$(P If the function is nested, the parameters returned are those of the
1782+
inner function, not the outer one.)
1783+
1784+
$(P When used inside a $(DDSUBLINK spec/statement, foreach-statement,
1785+
`foreach` statement) that calls an $(DDSUBLINK spec/statement,
1786+
foreach_over_struct_and_classes, `opApply` overload), the parameters
1787+
returned are those of the delegate passed to `opApply`.)
1788+
1789+
---
1790+
int add(int x, int y)
1791+
{
1792+
return x + y;
1793+
}
1794+
1795+
int forwardToAdd(int x, int y)
1796+
{
1797+
return add(__traits(parameters));
1798+
// equivalent to;
1799+
//return add(x, y);
1800+
}
1801+
1802+
int nestedExample(int x)
1803+
{
1804+
// outer function's parameters
1805+
static assert(typeof(__traits(parameters)).length == 1);
1806+
1807+
int add(int x, int y)
1808+
{
1809+
// inner function's parameters
1810+
static assert(typeof(__traits(parameters)).length == 2);
1811+
return x + y;
1812+
}
1813+
1814+
return add(x, x);
1815+
}
1816+
1817+
class C
1818+
{
1819+
int opApply(int delegate(size_t, C) dg)
1820+
{
1821+
if (dg(0, this)) return 1;
1822+
return 0;
1823+
}
1824+
}
1825+
1826+
void foreachExample(C c, int x)
1827+
{
1828+
foreach(idx; 0..5)
1829+
{
1830+
// foreach does not call opApply
1831+
static assert(is(typeof(__traits(parameters)) == AliasSeq!(C, int)));
1832+
}
1833+
foreach(idx, elem; c)
1834+
{
1835+
// foreach calls opApply
1836+
static assert(is(typeof(__traits(parameters)) == AliasSeq!(size_t, C)));
1837+
}
1838+
}
1839+
---
1840+
17751841
$(SPEC_SUBNAV_PREV_NEXT version, Conditional Compilation, errors, Error Handling)
17761842

17771843
Macros:

0 commit comments

Comments
 (0)