Skip to content
Merged
Show file tree
Hide file tree
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
99 changes: 50 additions & 49 deletions expression.dd
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,6 @@ $(GNAME PrimaryExpression):
$(GLINK StringLiterals)
$(GLINK ArrayLiteral)
$(GLINK AssocArrayLiteral)
$(GLINK Lambda)
$(GLINK FunctionLiteral)
$(GLINK AssertExpression)
$(GLINK MixinExpression)
Expand Down Expand Up @@ -1288,48 +1287,6 @@ $(GNAME ValueExpression):
are inserted as arguments in place of the tuple.
)

$(H4 Lambdas)

$(GRAMMAR
$(GNAME Lambda):
$(IDENTIFIER) $(D =>) $(GLINK AssignExpression)
$(D function)$(OPT) $(GLINK ParameterAttributes) $(D =>) $(GLINK AssignExpression)
$(D delegate)$(OPT) $(GLINK ParameterAttributes) $(D =>) $(GLINK AssignExpression)
)

$(P $(I Lambda)s are a shorthand syntax for $(GLINK FunctionLiteral)s.)

$(OL
$(LI $(P Just one $(IDENTIFIER) is rewritten to
$(GLINK2 declaration, Parameters):)

$(D $(LPAREN)) $(IDENTIFIER) $(D $(RPAREN))
)
$(LI $(P The following part $(D =>) $(I AssignExpression) is rewritten to
$(GLINK2 function, FunctionBody):)

$(D { return) $(I AssignExpression) $(D ; })
)
)

Example usage:
---
import std.stdio;

void main() {
auto i = 3;
auto twice = function (int x) => x * 2;
auto square = delegate (int x) => x * x;

auto n = 5;
auto mul_n = (int x) => x * n;

writeln(twice(i)); // prints 6
writeln(square(i)); // prints 9
writeln(mul_n(i)); // prints 15
}
---


$(H4 Function Literals)

Expand All @@ -1339,6 +1296,7 @@ $(GNAME FunctionLiteral):
$(D delegate) $(GLINK2 declaration, Type)$(OPT) $(GLINK ParameterAttributes) $(OPT) $(GLINK2 function, FunctionBody)
$(GLINK ParameterAttributes) $(GLINK2 function, FunctionBody)
$(GLINK2 function, FunctionBody)
$(GLINK Lambda)

$(GNAME ParameterAttributes):
$(GLINK2 declaration, Parameters)
Expand Down Expand Up @@ -1424,8 +1382,8 @@ $(GNAME ParameterAttributes):
}
-------------

If the type of a function literal can be uniquely determined from its context,
the parameter type inference is possible.
$(P If the type of a function literal can be uniquely determined from its context,
the parameter type inference is possible.)

-------------
void foo(int function(int) fp);
Expand All @@ -1439,8 +1397,8 @@ $(GNAME ParameterAttributes):
}
-------------

Anonymous delegates can behave like arbitrary statement literals.
For example, here an arbitrary statement is executed by a loop:
$(P Anonymous delegates can behave like arbitrary statement literals.
For example, here an arbitrary statement is executed by a loop:)

-------------
double test() {
Expand All @@ -1460,12 +1418,55 @@ $(GNAME ParameterAttributes):
}
-------------

When comparing with $(DDSUBLINK function, nested, nested functions),
$(P When comparing with $(DDSUBLINK function, nested, nested functions),
the $(D function) form is analogous to static
or non-nested functions, and the $(D delegate) form is
analogous to non-static nested functions. In other words,
a delegate literal can access stack variables in its enclosing
function, a function literal cannot.
function, a function literal cannot.)


$(H4 Lambdas)

$(GRAMMAR
$(GNAME Lambda):
$(IDENTIFIER) $(D =>) $(GLINK AssignExpression)
$(D function)$(OPT) $(GLINK ParameterAttributes) $(D =>) $(GLINK AssignExpression)
$(D delegate)$(OPT) $(GLINK ParameterAttributes) $(D =>) $(GLINK AssignExpression)
)

$(P $(I Lambda)s are a shorthand syntax for $(GLINK FunctionLiteral)s.)

$(OL
$(LI $(P Just one $(IDENTIFIER) is rewritten to
$(GLINK2 declaration, Parameters):)

$(D $(LPAREN)) $(IDENTIFIER) $(D $(RPAREN))
)
$(LI $(P The following part $(D =>) $(I AssignExpression) is rewritten to
$(GLINK2 function, FunctionBody):)

$(D { return) $(I AssignExpression) $(D ; })
)
)

Example usage:
---
import std.stdio;

void main() {
auto i = 3;
auto twice = function (int x) => x * 2;
auto square = delegate (int x) => x * x;

auto n = 5;
auto mul_n = (int x) => x * n;

writeln(twice(i)); // prints 6
writeln(square(i)); // prints 9
writeln(mul_n(i)); // prints 15
}
---


$(H4 Assert Expressions)
Expand Down
7 changes: 5 additions & 2 deletions function.dd
Original file line number Diff line number Diff line change
Expand Up @@ -629,11 +629,14 @@ void main() {

$(H4 Inline Functions)

There is no inline keyword. The compiler makes the decision whether to
$(P There is no inline keyword. The compiler makes the decision whether to
inline a function or not, analogously to the register keyword no
longer being relevant to a
compiler's decisions on enregistering variables.
(There is no register keyword either.)
(There is no register keyword either.))

$(P If a $(GLINK2 expression, FunctionLiteral) is immediately called,
its inlining would be enforced normally.)


$(H3 $(LNAME2 function-overloading, Function Overloading))
Expand Down