Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions spec/traits.dd
Original file line number Diff line number Diff line change
Expand Up @@ -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.)
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about adding:

If these constraints aren't fulfilled, the function is considered uncomparable and isSame returns false


$(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));

Copy link
Contributor

Choose a reason for hiding this comment

The 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));
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Down