@@ -80,6 +80,7 @@ $(GNAME TraitsKeyword):
80
80
$(RELATIVE_LINK2 compiles, $(D compiles))
81
81
$(RELATIVE_LINK2 toType, $(D toType))
82
82
$(RELATIVE_LINK2 initSymbol, $(D initSymbol))
83
+ $(RELATIVE_LINK2 parameters, $(D parameters))
83
84
84
85
$(GNAME TraitsArguments):
85
86
$(GLINK TraitsArgument)
@@ -1772,6 +1773,71 @@ $(H3 $(GNAME initSymbol))
1772
1773
}
1773
1774
---
1774
1775
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
+
1775
1841
$(SPEC_SUBNAV_PREV_NEXT version, Conditional Compilation, errors, Error Handling)
1776
1842
1777
1843
Macros:
0 commit comments