| Field | Value |
|---|---|
| DIP: | 1043 |
| Review Count: | 0 |
| Author: | Max Haughton, Adam D. Ruppe |
| Implementation: | dlang/dmd#11833 |
| Status: | Community Review Round 1 |
This DIP proposes a shortened syntax for function definitions. The following syntax is proposed:
int add(int x, int y) pure => x + y;The feature is already implemented in the D programming language as a preview.
A shortened syntax for function literals is already supported in the D programming language. For example:
const succ = (int x) => x + 1;is equivalent to
const succ = function(int x) { return x + 1; };Via a trivial change to the language, a similar syntax can be implemented for function definitions. This will bring more consistency to the syntax of function literals and definitions, saving the programmer a few keystrokes.
For example, consider a simple InputRange that produces a range of Ts that excludes one end ([from, to)).
An implementation in D without the proposed feature is 14 lines:
struct LongerExclusiveRange(T)
{
T from, to;
invariant(from <= to);
bool empty() {
return from == to;
}
void popFront() {
++from;
}
T front() {
return from;
}
}An implementation utilizing the proposed feature is only 8 lines:
struct ExclusiveRange(T)
{
T from, to;
invariant(from <= to);
bool empty() => from == to;
auto popFront() => ++from;
T front() => from;
}The syntax proposed by this DIP can also make the writing of function compositions more direct—if there is nothing that requires the use of braces, then they can be elided. For example, take this arbitrary composition of ranges:
auto doesWork()
=> iota(1, 100)
.map!(x => x + 1)
.filter!(x => x > 4)
.each!writeln;With the shortened methods syntax, this function terminates syntactically in the same place it does semantically.
The call to each is where the function's work ends. This DIP allows the programmer to end the function's syntax there too,
saving the author some keystrokes and reducing the visual noise the reader must process.
This DIP has the potential to result in a post-facto blessing of a preview feature already supported by the compiler. The existing implementation was written by Adam D. Ruppe.
The proposed feature is present in the C# programming language, where instances of its use are referred to as expression-bodied members.
The C# documentation demonstrates an idiomatic example of the proposed feature, reproduced here:
public class Location
{
private string locationName;
public Location(string name)
{
locationName = name;
}
public string Name => locationName;
}Note that the Name function is a simple one-liner that returns the locationName member. Such one-liners are common in C#,
where programmers are encouraged to provide access to member variables via member functions.
The proposed semantics are a simple example of what is referred to in the theory of programming language implementation as lowering:
FunctionDeclarator => AssignExpression;shall be rewritten to
FunctionDeclarator
{
return AssignExpression;
}Given that constructors and destructors cannot have return values, the implementation should reject any attempt to implement a constructor or destructor using the shortened method syntax and provide a meaningful error message.
The proposed feature requires the following grammar changes:
FunctionBody:
SpecifiedFunctionBody
MissingFunctionBody
+ ShortenedFunctionBody
...
+ShortenedFunctionBody:
+ => AssignExpression ;It has been noted that the current implementation allows the use of function contracts with this syntax. Function contracts are not possible with function literals, so this DIP does not address the issue. It is mentioned here both for future reference and in case review determines the proposal should be enhanced to explicitly address function contracts in the presence of shortened method syntax.
- The syntax was mentioned on the D Bugzilla in 2011. The dialectics in this thread can be considered a pre-review.
- Shortened Methods are documented in the specification
- A dmd changelog entry was added
Copyright (c) 2022 by the D Language Foundation
Licensed under Creative Commons Zero 1.0
The DIP Manager will supplement this section with a summary of each review stage of the DIP process beyond the Draft Review.