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

Comments

Projects
None yet
7 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

This comment has been minimized.

Show comment
Hide comment
@mrjavascript

mrjavascript Sep 3, 2015

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

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

@askoning

This comment has been minimized.

Show comment
Hide comment
@askoning

askoning Sep 23, 2015

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

askoning commented Sep 23, 2015

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

This comment has been minimized.

Show comment
Hide comment
@bb010g

bb010g Mar 10, 2016

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

bb010g commented Mar 10, 2016

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

@btiernay

This comment has been minimized.

Show comment
Hide comment
@btiernay

btiernay Mar 20, 2016

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

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

This comment has been minimized.

Show comment
Hide comment
@rspilker

rspilker Oct 17, 2016

Collaborator

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

Collaborator

rspilker commented Oct 17, 2016

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

@douglarek

This comment has been minimized.

Show comment
Hide comment
@douglarek

douglarek Apr 11, 2017

Still fails in Lombok 1.16.16 (latest stable version)

Still fails in Lombok 1.16.16 (latest stable version)

@jdigger

This comment has been minimized.

Show comment
Hide comment
@jdigger

jdigger Apr 14, 2017

FWIW, it works great for me in 1.16.12.

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