-
Notifications
You must be signed in to change notification settings - Fork 48
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
Single line functions #548
Comments
One possibility would be to do that using using the arrow syntax: https://dlang.org/changelog/2.096.0.html#shortfunctions The way this could work is that if dfmt detects that the function contains a single ReturnType functionName(Args args) =>
some.long.expression; |
I personally prefer not to use arrow syntax. |
The reason it was added was that it is already in use with lambdas. It closes a hole in the syntax to make it more consistent. |
dfmt never changes the source code tokens or does refactorings, it only manipulates whitespace. This has been the case for a long time, maybe even since start of the project, so other tools also might rely on it not manipulating any tokens. So I would strongly deny the formatter changing short return functions to A refactoring would better fit in dfix or serve-d, where other refactorings are already implemented. I agree with this issue that there should be an option to keep short methods short. I would be for adding an extra option so that one-line methods are always kept one-line, even if they break the line length limit. D-Scanner or other tools can then complain if it's longer than the configured max line length and let the user fix it. I think that way would be better as otherwise if you had int displayWidth() const @property { return _displayWidth; }
int displayHeight() const @property { return _displayHeight; } and the line length limit would be just after the first line and before the second line, it would result in the first function staying one-line and the second one being split across multiple lines, which is ugly imo. |
That's alright. I'm all for syntactic freedom :) It kind of reminds me of Java's improvements to |
The same online addition may potentially be interesting for switch statement as well when single line expressions are used, Especially as dlang also has Example: switch (soor) {
case "SCALAR":
return 1;
case "VEC2":
return 2;
case "VEC3":
return 3;
case "VEC4":
return 4;
case "MAT2":
return 4;
case "MAT3":
return 9;
case "MAT4":
return 16;
default:
assert(0, "Unsupported accessors.type: " + type);
} Compared to switch (soor) {
case "SCALAR": return 1;
case "VEC2": return 2;
case "VEC3": return 3;
case "VEC4": return 4;
case "MAT2": return 4;
case "MAT3": return 9;
case "MAT4": return 16;
default: assert(0, "Unsupported accessors.type: " + type);
} |
Agreed, and more generally, perhaps all |
Additionally, I'd like to add statements like |
Requested enhancement to add the option of keeping single-line functions on one line. This is a feature in many other formatting tools & can improve readability when many tiny functions are included.
Current:
Becomes:
The text was updated successfully, but these errors were encountered: