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

val inside Java 8 lambda expressions #911

Closed
mrjavascript opened this issue Sep 3, 2015 · 7 comments
Closed

val inside Java 8 lambda expressions #911

mrjavascript opened this issue Sep 3, 2015 · 7 comments

Comments

@mrjavascript
Copy link

I'm building a project using Gradle 2.5 and JDK 1.8.

Something outside of a lambda compiles correctly. e.g.

         val params = new MapSqlParameterSource();
  params.addValue("patientId", patientId);

However if trying to compile something inside a lambda:

                          (rs, rowNum) -> {
                  val holder = new PatientPhoneNumberHolder(patientPhoneNumberRepository.findOne(rs.getInt("record_id")));
                  holder.setPhoneNumber(rs.getString("phone_number"));
                  return holder;
          }

I receive this error:

error: incompatible types: PatientPhoneNumberHolder cannot be converted to val

Is Lombok supported with lambda expressions? I recall someone running into an issue with "val" when dealing with nested classes... not sure if it's somewhat related with this issue.

@mrjavascript
Copy link
Author

I am also using the latest version of Lombok as well (1.16.6)

@askoning
Copy link
Collaborator

Indeed. This does not compile and indicates that the val inside the lambda expression is not even visited:

import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;
import lombok.val;

public class LambdaVal {
    public static void main(String[] args) {
        List<String> strings = new ArrayList<String>();
        List<String> result = strings.stream().filter(s -> {
            val r = Boolean.TRUE;
            return r;
        }).collect(Collectors.toList());
    }
}

@bb010g
Copy link

bb010g commented Mar 10, 2016

Still fails on 1.16.8 on javac. Works fine with ecj.

@btiernay
Copy link

I have been experiencing this for a while. I always avoid using val inside a lambda because of it. It would be great if this could be fixed because it would greatly reduce type annotation noise in a lambda closure. For now, I just create methods that use val and use method references in streams, etc..

@rspilker
Copy link
Collaborator

Is fixed in the edge release and will be part of the next release. Can someone verify this on their code base?

@douglarek
Copy link

Still fails in Lombok 1.16.16 (latest stable version)

@jdigger
Copy link

jdigger commented Apr 14, 2017

FWIW, it works great for me in 1.16.12.

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

7 participants