Lombok: val inside Java 8 lambda expressions

Created on 3 Sep 2015  路  7Comments  路  Source: projectlombok/lombok

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.

Most helpful comment

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..

All 7 comments

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

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());
    }
}

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

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..

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

Still fails in Lombok 1.16.16 (latest stable version)

FWIW, it works great for me in 1.16.12.

Was this page helpful?
0 / 5 - 0 ratings