Skip to content

Commit deae0dc

Browse files
committed
Update traits.dd
1 parent 26789a2 commit deae0dc

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

spec/traits.dd

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,68 @@ void main()
10201020
$(P If the two arguments are expressions made up of literals
10211021
or enums that evaluate to the same value, true is returned.)
10221022

1023+
$(P If the two arguments are both lambda functions (or aliases
1024+
to lambda functions), then they are compared for equality. For
1025+
the comparison to be computed correctly, the following conditions
1026+
must be met for both lambda functions:)
1027+
1028+
$(OL
1029+
$(LI The lambda function arguments must not have a template
1030+
instantiation as an explicit argument type. Any other argument
1031+
types (basic, user-defined, template) are supported.)
1032+
$(LI The lambda function body must contain a single expression
1033+
(no return statement) which contains only numeric values,
1034+
manifest constants, enum values and function arguments. If the
1035+
expression contains local variables, function calls or return
1036+
statements, the function is considered uncomparable.)
1037+
)
1038+
1039+
$(P If these constraints aren't fulfilled, the function is considered
1040+
incomparable and `isSame` returns $(D false).)
1041+
1042+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
1043+
---
1044+
void main()
1045+
{
1046+
static assert(__traits(isSame, (a, b) => a + b, (c, d) => c + d));
1047+
static assert(__traits(isSame, a => ++a, b => ++b));
1048+
static assert(!__traits(isSame, (int a, int b) => a + b, (a, b) => a + b));
1049+
static assert(__traits(isSame, (a, b) => a + b + 10, (c, d) => c + d + 10));
1050+
1051+
// lambdas accessing local variables are considered incomparable
1052+
int b;
1053+
static assert(!__traits(isSame, a => a + b, a => a + b));
1054+
1055+
// lambdas calling a function inside their body are considered incomparable
1056+
int f() { return 3;}
1057+
static assert(!__traits(isSame, a => a + f(), a => a + f()));
1058+
1059+
class A
1060+
{
1061+
int a;
1062+
this(int a)
1063+
{
1064+
this.a = a;
1065+
}
1066+
}
1067+
1068+
class B
1069+
{
1070+
int a;
1071+
this(int a)
1072+
{
1073+
this.a = a;
1074+
}
1075+
}
1076+
1077+
static assert(__traits(isSame, (A a) => ++a.a, (A b) => ++b.a));
1078+
// lambdas with different data types are considered incomparable,
1079+
// even if the memory layout is the same
1080+
static assert(!__traits(isSame, (A a) => ++a.a, (B a) => ++a.a));
1081+
}
1082+
---
1083+
)
1084+
10231085
$(H2 $(GNAME compiles))
10241086

10251087
$(P Returns a bool $(D true) if all of the arguments

0 commit comments

Comments
 (0)