@@ -1677,33 +1677,40 @@ void main()
1677
1677
1678
1678
$(H3 $(GNAME isSame))
1679
1679
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).)
1682
1684
1683
1685
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
1684
1686
---
1685
- import std.stdio;
1686
-
1687
1687
struct S { }
1688
1688
1689
1689
int foo();
1690
1690
int bar();
1691
1691
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));
1701
1701
---
1702
1702
)
1703
1703
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.)
1706
1706
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
+ )
1707
1714
$(P If the two arguments are both
1708
1715
$(DDSUBLINK spec/expression, function_literals, lambda functions) (or aliases
1709
1716
to lambda functions), then they are compared for equality. For
@@ -1722,11 +1729,20 @@ void main()
1722
1729
)
1723
1730
1724
1731
$(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).)
1726
1733
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
+ )
1727
1742
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
1728
1743
---
1729
1744
int f() { return 2; }
1745
+
1730
1746
void test(alias pred)()
1731
1747
{
1732
1748
// f() from main is a different function from top-level f()
@@ -1735,11 +1751,6 @@ void test(alias pred)()
1735
1751
1736
1752
void main()
1737
1753
{
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
-
1743
1754
// lambdas accessing local variables are considered incomparable
1744
1755
int b;
1745
1756
static assert(!__traits(isSame, a => a + b, a => a + b));
@@ -1748,7 +1759,11 @@ void main()
1748
1759
int f() { return 3;}
1749
1760
static assert(__traits(isSame, a => a + f(), a => a + f()));
1750
1761
test!((int a) => a + f())();
1751
-
1762
+ }
1763
+ ---
1764
+ )
1765
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
1766
+ ---
1752
1767
class A
1753
1768
{
1754
1769
int a;
@@ -1771,30 +1786,25 @@ void main()
1771
1786
// lambdas with different data types are considered incomparable,
1772
1787
// even if the memory layout is the same
1773
1788
static assert(!__traits(isSame, (A a) => ++a.a, (B a) => ++a.a));
1774
- }
1775
- ---
1776
- )
1789
+ ---
1790
+ )
1777
1791
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
1779
1793
two tuples, after expansion, have the same length and if each pair
1780
1794
of nth argument respects the constraints previously specified.)
1781
1795
1782
1796
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
1783
1797
---
1784
- import std.stdio;
1785
1798
import std.meta;
1786
1799
1787
1800
struct S { }
1788
1801
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)));
1798
1808
---
1799
1809
)
1800
1810
0 commit comments