Skip to content

Commit a2367d0

Browse files
authored
Merge pull request #3750 from ntrel/traits-compiles
[spec] Improve `__traits(compiles)` docs
2 parents fde1bba + de169c6 commit a2367d0

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

spec/traits.dd

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,44 +1845,51 @@ $(H3 $(GNAME compiles))
18451845
compile (are semantically correct).
18461846
The arguments can be symbols, types, or expressions that
18471847
are syntactically correct.
1848-
The arguments cannot be statements or declarations.
1848+
The arguments cannot be statements or declarations - instead
1849+
these can be wrapped in a $(DDSUBLINK spec/expression, function_literals,
1850+
function literal) expression.
18491851
)
18501852

18511853
$(P If there are no arguments, the result is $(D false).)
18521854

18531855
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
18541856
---
1855-
import std.stdio;
1857+
static assert(!__traits(compiles));
1858+
static assert(__traits(compiles, 1 + 1)); // expression
1859+
static assert(__traits(compiles, typeof(1))); // type
1860+
static assert(__traits(compiles, object)); // symbol
1861+
static assert(__traits(compiles, 1, 2, 3, int, long));
1862+
static assert(!__traits(compiles, 3[1])); // semantic error
1863+
static assert(!__traits(compiles, 1, 2, 3, int, long, 3[1]));
1864+
1865+
enum n = 3;
1866+
// wrap a declaration/statement in a function literal
1867+
static assert(__traits(compiles, { int[n] arr; }));
1868+
static assert(!__traits(compiles, { foreach (e; n) {} }));
18561869

18571870
struct S
18581871
{
18591872
static int s1;
18601873
int s2;
18611874
}
18621875

1876+
static assert(__traits(compiles, S.s1 = 0));
1877+
static assert(!__traits(compiles, S.s2 = 0));
1878+
static assert(!__traits(compiles, S.s3));
1879+
18631880
int foo();
1864-
int bar();
18651881

1866-
void main()
1867-
{
1868-
writeln(__traits(compiles)); // false
1869-
writeln(__traits(compiles, foo)); // true
1870-
writeln(__traits(compiles, foo + 1)); // true
1871-
writeln(__traits(compiles, &foo + 1)); // false
1872-
writeln(__traits(compiles, typeof(1))); // true
1873-
writeln(__traits(compiles, S.s1)); // true
1874-
writeln(__traits(compiles, S.s3)); // false
1875-
writeln(__traits(compiles, 1,2,3,int,long,std)); // true
1876-
writeln(__traits(compiles, 3[1])); // false
1877-
writeln(__traits(compiles, 1,2,3,int,long,3[1])); // false
1878-
}
1882+
static assert(__traits(compiles, foo));
1883+
static assert(__traits(compiles, foo + 1)); // call foo with optional parens
1884+
static assert(!__traits(compiles, &foo + 1));
18791885
---
18801886
)
18811887

18821888
$(P This is useful for:)
18831889

18841890
$(UL
1885-
$(LI Giving better error messages inside generic code than
1891+
$(LI Giving better error messages (using $(DDSUBLINK spec/version, static-assert,
1892+
`static assert`)) inside generic code than
18861893
the sometimes hard to follow compiler ones.)
18871894
$(LI Doing a finer grained specialization than template
18881895
partial specialization allows for.)

0 commit comments

Comments
 (0)