val inside Java 8 lambda expressions #911

Closed
mrjavascript opened this Issue Sep 3, 2015 · 5 comments

Projects

None yet

5 participants

@mrjavascript

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

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

@askoning
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
bb010g commented Mar 10, 2016

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

@btiernay

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
Collaborator

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

@rspilker rspilker closed this Oct 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment