Skip to content

Commit 2942eb0

Browse files
authored
Improve __traits(isSame) spec (#3537)
Use 'the result is' not 'returns'. Mention an alias may resolve to the same symbol. Use static asserts instead of writeln. Compare object instead of std so no import needed. Add example comparing expressions. Split up large lambda example.
1 parent 0bf3701 commit 2942eb0

File tree

1 file changed

+46
-36
lines changed

1 file changed

+46
-36
lines changed

spec/traits.dd

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,33 +1677,40 @@ void main()
16771677

16781678
$(H3 $(GNAME isSame))
16791679

1680-
$(P Takes two arguments and returns bool $(D true) if they
1681-
are the same symbol, $(D false) if not.)
1680+
$(P Compares two arguments and evaluates to `bool`.)
1681+
1682+
$(P The result is `true` if the two arguments are the same symbol
1683+
(once aliases are resolved).)
16821684

16831685
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
16841686
---
1685-
import std.stdio;
1686-
16871687
struct S { }
16881688

16891689
int foo();
16901690
int bar();
16911691

1692-
void main()
1693-
{
1694-
writeln(__traits(isSame, foo, foo)); // true
1695-
writeln(__traits(isSame, foo, bar)); // false
1696-
writeln(__traits(isSame, foo, S)); // false
1697-
writeln(__traits(isSame, S, S)); // true
1698-
writeln(__traits(isSame, std, S)); // false
1699-
writeln(__traits(isSame, std, std)); // true
1700-
}
1692+
static assert(__traits(isSame, foo, foo));
1693+
static assert(!__traits(isSame, foo, bar));
1694+
static assert(!__traits(isSame, foo, S));
1695+
static assert(__traits(isSame, S, S));
1696+
static assert(!__traits(isSame, object, S));
1697+
static assert(__traits(isSame, object, object));
1698+
1699+
alias daz = foo;
1700+
static assert(__traits(isSame, foo, daz));
17011701
---
17021702
)
17031703

1704-
$(P If the two arguments are expressions made up of literals
1705-
or enums that evaluate to the same value, true is returned.)
1704+
$(P The result is `true` if the two arguments are expressions
1705+
made up of literals or enums that evaluate to the same value.)
17061706

1707+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
1708+
---
1709+
enum e = 3;
1710+
static assert(__traits(isSame, (e), 3));
1711+
static assert(__traits(isSame, 5, 2 + e));
1712+
---
1713+
)
17071714
$(P If the two arguments are both
17081715
$(DDSUBLINK spec/expression, function_literals, lambda functions) (or aliases
17091716
to lambda functions), then they are compared for equality. For
@@ -1722,11 +1729,20 @@ void main()
17221729
)
17231730

17241731
$(P If these constraints aren't fulfilled, the function is considered
1725-
incomparable and `isSame` returns $(D false).)
1732+
incomparable and the result is $(D false).)
17261733

1734+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
1735+
---
1736+
static assert(__traits(isSame, (a, b) => a + b, (c, d) => c + d));
1737+
static assert(__traits(isSame, a => ++a, b => ++b));
1738+
static assert(!__traits(isSame, (int a, int b) => a + b, (a, b) => a + b));
1739+
static assert(__traits(isSame, (a, b) => a + b + 10, (c, d) => c + d + 10));
1740+
---
1741+
)
17271742
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
17281743
---
17291744
int f() { return 2; }
1745+
17301746
void test(alias pred)()
17311747
{
17321748
// f() from main is a different function from top-level f()
@@ -1735,11 +1751,6 @@ void test(alias pred)()
17351751

17361752
void main()
17371753
{
1738-
static assert(__traits(isSame, (a, b) => a + b, (c, d) => c + d));
1739-
static assert(__traits(isSame, a => ++a, b => ++b));
1740-
static assert(!__traits(isSame, (int a, int b) => a + b, (a, b) => a + b));
1741-
static assert(__traits(isSame, (a, b) => a + b + 10, (c, d) => c + d + 10));
1742-
17431754
// lambdas accessing local variables are considered incomparable
17441755
int b;
17451756
static assert(!__traits(isSame, a => a + b, a => a + b));
@@ -1748,7 +1759,11 @@ void main()
17481759
int f() { return 3;}
17491760
static assert(__traits(isSame, a => a + f(), a => a + f()));
17501761
test!((int a) => a + f())();
1751-
1762+
}
1763+
---
1764+
)
1765+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
1766+
---
17521767
class A
17531768
{
17541769
int a;
@@ -1771,30 +1786,25 @@ void main()
17711786
// lambdas with different data types are considered incomparable,
17721787
// even if the memory layout is the same
17731788
static assert(!__traits(isSame, (A a) => ++a.a, (B a) => ++a.a));
1774-
}
1775-
---
1776-
)
1789+
---
1790+
)
17771791

1778-
$(P If the two arguments are tuples then `isSame` returns `true` if the
1792+
$(P If the two arguments are tuples then the result is `true` if the
17791793
two tuples, after expansion, have the same length and if each pair
17801794
of nth argument respects the constraints previously specified.)
17811795

17821796
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
17831797
---
1784-
import std.stdio;
17851798
import std.meta;
17861799

17871800
struct S { }
17881801

1789-
void main()
1790-
{
1791-
// true, like __traits(isSame(0,0)) && __traits(isSame(1,1))
1792-
writeln(__traits(isSame, AliasSeq!(0,1), AliasSeq!(0,1)));
1793-
// false, like __traits(isSame(S,std.meta)) && __traits(isSame(1,1))
1794-
writeln(__traits(isSame, AliasSeq!(S,1), AliasSeq!(std.meta,1)));
1795-
// false, the length of the sequences is different
1796-
writeln(__traits(isSame, AliasSeq!(1), AliasSeq!(1,2)));
1797-
}
1802+
// like __traits(isSame,0,0) && __traits(isSame,1,1)
1803+
static assert(__traits(isSame, AliasSeq!(0,1), AliasSeq!(0,1)));
1804+
// like __traits(isSame,S,std.meta) && __traits(isSame,1,1)
1805+
static assert(!__traits(isSame, AliasSeq!(S,1), AliasSeq!(std.meta,1)));
1806+
// the length of the sequences is different
1807+
static assert(!__traits(isSame, AliasSeq!(1), AliasSeq!(1,2)));
17981808
---
17991809
)
18001810

0 commit comments

Comments
 (0)