Bug 466723 - [1.8][typing] Adding curly braces to lambda expression body
Summary: [1.8][typing] Adding curly braces to lambda expression body
Status: REOPENED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Text (show other bugs)
Version: 4.5   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-Text-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2015-05-07 08:17 EDT by Lukas Eder CLA
Modified: 2024-03-10 10:32 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Lukas Eder CLA 2015-05-07 08:17:54 EDT
Consider the following program:

----------------------------------------------------------------
import java.util.stream.IntStream;

public class Test {
    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) System.out.println(i);
        IntStream.range(0, 3).forEach(i -> System.out.println(i));
    }
}
----------------------------------------------------------------

It is a common thing to add curly braces where there was a single statement before, and then hit enter. 

Adding curly braces:

----------------------------------------------------------------
        for (int i = 0; i < 3; i++) { System.out.println(i);
        IntStream.range(0, 3).forEach(i -> { System.out.println(i));
----------------------------------------------------------------

Hitting enter:

----------------------------------------------------------------
        for (int i = 0; i < 3; i++) { 
            System.out.println(i);
        }
        IntStream.range(0, 3).forEach(i -> {

        }System.out.println(i));
----------------------------------------------------------------

While the for loop correctly enclosed the println statement in the curly braces, the lambda expression got broken. The expected behaviour would be this (note also the added ";")

----------------------------------------------------------------
        IntStream.range(0, 3).forEach(i -> {
            System.out.println(i);
        });
----------------------------------------------------------------
Comment 1 Noopur Gupta CLA 2015-05-07 08:45:22 EDT
It is not sufficient to add the braces and semicolon correctly.

For example:

IntStream.range(0, 3).mapToObj(i -> Integer.toHexString(i));

should result in:

IntStream.range(0, 3).mapToObj(i -> {
    return Integer.toHexString(i);
});

To get the expected result, you should use the quick assist "Change body expression to block" at lambda expression.

This bug can be kept to just move the lambda body within braces when Enter is pressed.
Comment 2 Lukas Eder CLA 2015-05-07 08:53:42 EDT
You're right, it's not sufficient to just move the body and add semicolons. I only documented the case of a void-compatible lambda expression (e.g. a Consumer).

But since the intent of hitting enter after those initial curly braces is rather clear, I still vote for handling the action just the same as the quick assist "Change body expression to block". It would be a very convenient action, rather than hitting the menu and making a choice among several options...
Comment 3 Eclipse Genie CLA 2019-11-04 07:01:07 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 4 Lukas Eder CLA 2020-03-20 11:33:17 EDT
> This bug can be kept to just move the lambda body within braces when Enter is pressed.

But then it got auto-closed (which I still don't find a very good practice). I'd still love to see this fixed.
Comment 5 Eclipse Genie CLA 2022-03-15 12:46:28 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 6 Eclipse Genie CLA 2024-03-10 10:32:55 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.