Skip to content
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

LambdaIsMethodReference mutator could introduce side effects #847

Open
mches opened this issue Oct 9, 2024 · 2 comments
Open

LambdaIsMethodReference mutator could introduce side effects #847

mches opened this issue Oct 9, 2024 · 2 comments

Comments

@mches
Copy link
Contributor

mches commented Oct 9, 2024

An expression lambda like s -> b.append("/").append(s) can't be converted to a method reference because b.append("/") has a side effect

String a = null;
StringBuilder b = new StringBuilder();
Optional.ofNullable(a).ifPresent(s -> b.append("/").append(s));
System.out.println(b);

Output:


↓↓↓

String a = null;
StringBuilder b = new StringBuilder();
Optional.ofNullable(a).ifPresent(b.append("/")::append);
System.out.println(b);

Output:

/

Seems safer to assume a call chain produces a side effect and give up on converting such a lambda expression to method reference.

@blacelle
Copy link
Member

blacelle commented Oct 9, 2024

Fixed in #849

@mches
Copy link
Contributor Author

mches commented Oct 10, 2024

Bravo! 👏🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants