Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » switch from "lambda expression" to "method reference"
switch from "lambda expression" to "method reference" [message #1758833] Mon, 03 April 2017 15:13 Go to next message
Nicola  Zanaga is currently offline Nicola ZanagaFriend
Messages: 48
Registered: July 2009
Member
Hello, context assist now always proposes to switch from "lambda expression" to "method reference" when it's possibile, but I think it's wrong because not all conversions are the same thing.

When working with non-final class variables, it shouldn't propose to switch to "method reference".

Below a snippet that produces different results:


public class TestClass {
    static class TestClassInt {
        int value;

        public TestClassInt(int i) {
            value = i;
        }

        public void printMe() {
            System.out.println(String.valueOf(value));
        }
    }

    TestClassInt var1;
    Runnable     r1;
    Runnable     r2;

    public TestClass() {
        var1 = new TestClassInt(1);

        r1 = () -> var1.printMe();
        r2 = var1::printMe;

        var1 = new TestClassInt(2);
    }

    private void print() {
        r1.run();
        r2.run();
    }

    public static void main(String[] args) {
        new TestClass().print();
    }

}


Re: switch from "lambda expression" to "method reference" [message #1760762 is a reply to message #1758833] Tue, 02 May 2017 16:38 Go to previous message
Jay Arthanareeswaran is currently offline Jay ArthanareeswaranFriend
Messages: 15
Registered: July 2009
Junior Member
Are you talking about this line of code?

r1 = () -> var1.printMe();

There's no compiler error when this is converted to a method reference. So, I don't see why the conversion can't be offered.
Previous Topic:Is JRE 9 compatibile with Eclipse 3.8 RCP Application?
Next Topic:JDT - Find startline of method excluding Javadocs
Goto Forum:
  


Current Time: Fri Dec 15 10:30:47 GMT 2017

Powered by FUDForum. Page generated in 0.01589 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software