-
-
Notifications
You must be signed in to change notification settings - Fork 388
Update docs to highlight lambda comparison #2146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1020,6 +1020,68 @@ void main() | |
| $(P If the two arguments are expressions made up of literals | ||
| or enums that evaluate to the same value, true is returned.) | ||
|
|
||
| $(P If the two arguments are both lambda functions (or aliases | ||
| to lambda functions), then they are compared for equality. For | ||
| the comparison to be computed correctly, the following conditions | ||
| must be met for both lambda functions:) | ||
|
|
||
| $(OL | ||
| $(LI The lambda function arguments must not have a template | ||
| instantiation as an explicit argument type. Any other argument | ||
| types (basic, user-defined, template) are supported.) | ||
| $(LI The lambda function body must contain a single expression | ||
| (no return statement) which contains only numeric values, | ||
| manifest constants, enum values and function arguments. If the | ||
| expression contains local variables, function calls or return | ||
| statements, the function is considered uncomparable.) | ||
| ) | ||
|
|
||
| $(P If these constraints aren't fulfilled, the function is considered | ||
| incomparable and `isSame` returns $(D false).) | ||
|
|
||
| $(SPEC_RUNNABLE_EXAMPLE_COMPILE | ||
| --- | ||
| void main() | ||
| { | ||
| static assert(__traits(isSame, (a, b) => a + b, (c, d) => c + d)); | ||
| static assert(__traits(isSame, a => ++a, b => ++b)); | ||
| static assert(!__traits(isSame, (int a, int b) => a + b, (a, b) => a + b)); | ||
| static assert(__traits(isSame, (a, b) => a + b + 10, (c, d) => c + d + 10)); | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. // lambdas accessing local variables are considered incomparable |
||
| // lambdas accessing local variables are considered incomparable | ||
| int b; | ||
| static assert(!__traits(isSame, a => a + b, a => a + b)); | ||
|
|
||
| // lambdas calling a function inside their body are considered incomparable | ||
| int f() { return 3;} | ||
| static assert(!__traits(isSame, a => a + f(), a => a + f())); | ||
|
|
||
| class A | ||
| { | ||
| int a; | ||
| this(int a) | ||
| { | ||
| this.a = a; | ||
| } | ||
| } | ||
|
|
||
| class B | ||
| { | ||
| int a; | ||
| this(int a) | ||
| { | ||
| this.a = a; | ||
| } | ||
| } | ||
|
|
||
| static assert(__traits(isSame, (A a) => ++a.a, (A b) => ++b.a)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. // lambdas with different data types are incomparable - even if have the same memory layout |
||
| // lambdas with different data types are considered incomparable, | ||
| // even if the memory layout is the same | ||
| static assert(!__traits(isSame, (A a) => ++a.a, (B a) => ++a.a)); | ||
| } | ||
| --- | ||
| ) | ||
|
|
||
| $(H2 $(GNAME compiles)) | ||
|
|
||
| $(P Returns a bool $(D true) if all of the arguments | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about adding: