Function calls that take a single lambda should not be on a new line
See original GitHub issueOverview
When there is a only a single argument in a function call it seems unnecessary to place it onto a new line. This misaligns with how other prettier languages work, like in Javascript:
A single argument taking a lambda
When there are multiple arguments, e.g. two lambdas
Steps to reproduce
Prettier-Java 0.8.0
Stream methods (map, filter, etc)
Input:
final List<Integer> values = Stream.of(1, 2)
.map(n -> {
// testing method
return n * 2;
})
.collect(Collectors.toList());
Output:
final List<Integer> values = Stream
.of(1, 2)
.map(
n -> {
// testing method
return n * 2;
}
)
.collect(Collectors.toList());
Expected behavior:
final List<Integer> values = Stream
.of(1, 2)
.map(n -> {
// testing method
return n * 2;
})
.collect(Collectors.toList());
Completable Futures (single argument)
Input:
CompletableFuture.supplyAsync(() -> {
// some processing
return 2;
});
Output:
CompletableFuture.supplyAsync(
() -> {
// some processing
return 2;
}
);
Expected behavior:
CompletableFuture.supplyAsync(() -> {
// some processing
return 2;
});
Completable Future (Multiple Arguments)
Input:
CompletableFuture.supplyAsync(() -> {
// some processing
return 2;
}, executor);
Output:
CompletableFuture.supplyAsync(
() -> {
// some processing
return 2;
},
executor
);
Expected behavior:
CompletableFuture.supplyAsync(
() -> {
// some processing
return 2;
},
executor
);
Function arguments that exceed width
Input:
myFunction((one, two, three, four, five, six) -> {
// some processing
return 2;
});
Output:
myFunction(
(one, two, three, four, five, six) -> {
// some processing
return 2;
}
);
Expected behavior:
myFunction(
(one, two, three, four, five, six) -> {
// some processing
return 2;
}
);
Issue Analytics
- State:
- Created 3 years ago
- Reactions:7
- Comments:5 (3 by maintainers)
Top Results From Across the Web
lambda function in different lines - Stack Overflow
I feel when I write it is ok to use lambda function in one line, is quick and good, but when I check...
Read more >CS 61A Higher-Order Functions, Self Reference Fall 2021
Unlike def statements, lambda expressions can be used as an operator or an operand to a call expression. This is because they are...
Read more >Best practices for working with AWS Lambda functions
This is especially important when your Lambda function makes network calls to resources that may not handle Lambda's scaling. Use most-restrictive permissions ...
Read more >Best practices when you use Lambda expressions in Java
Braces and return statements are optional in one-line lambda bodies. This means, that they can be omitted for clarity and conciseness. Do this:...
Read more >Lambda expressions and anonymous functions | Microsoft Learn
Use the lambda declaration operator => to separate the lambda's parameter ... The body of an expression lambda can consist of a method...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If I’m not mistaken, I already had a look, and it was not so easy to fix, as the syntax tree is quite complex around lambdas. But it would definitely be better formatted on a single line. I’ll try to have a look soon, I may have more inspiration 😃
OK, increasing the bounty to $300