Skip to content

Commit effa600

Browse files
RazvanN7wilzbach
authored andcommitted
Update examples for traits(isSame) to accommodate the use of function calls (#2231)
1 parent 7800636 commit effa600

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

spec/traits.dd

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,16 +1053,23 @@ void main()
10531053
types (basic, user-defined, template) are supported.)
10541054
$(LI The lambda function body must contain a single expression
10551055
(no return statement) which contains only numeric values,
1056-
manifest constants, enum values and function arguments. If the
1057-
expression contains local variables, function calls or return
1058-
statements, the function is considered uncomparable.)
1056+
manifest constants, enum values, function arguments and function
1057+
calls. If the expression contains local variables or return
1058+
statements, the function is considered incomparable.)
10591059
)
10601060

10611061
$(P If these constraints aren't fulfilled, the function is considered
10621062
incomparable and `isSame` returns $(D false).)
10631063

10641064
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
10651065
---
1066+
int f() { return 2; }
1067+
void test(alias pred)()
1068+
{
1069+
// f() from main is a different function from top-level f()
1070+
static assert(!__traits(isSame, (int a) => a + f(), pred));
1071+
}
1072+
10661073
void main()
10671074
{
10681075
static assert(__traits(isSame, (a, b) => a + b, (c, d) => c + d));
@@ -1074,6 +1081,11 @@ void main()
10741081
int b;
10751082
static assert(!__traits(isSame, a => a + b, a => a + b));
10761083

1084+
// lambdas calling other functions are comparable
1085+
int f() { return 3;}
1086+
static assert(__traits(isSame, a => a + f(), a => a + f()));
1087+
test!((int a) => a + f())();
1088+
10771089
class A
10781090
{
10791091
int a;

0 commit comments

Comments
 (0)