Lombok-intellij-plugin: Cannot use 'val' here because initializer expression does not have a representable type: Type cannot be resolved

Created on 21 Jun 2018  路  3Comments  路  Source: mplushnikov/lombok-intellij-plugin

Lombok 1.18.0

Works:

Stream.of(DocumentCapability.DOCUMENT_TO_PDF, DocumentCapability.DOCUMENT_TO_TEXT)
            .map(
                c -> {
                  CapabilityStatusRecord s = new CapabilityStatusRecord();
                  s.setCapability(c);
                  return s;
                })
            .collect(Collectors.toList());

Doesn't work:

Stream.of(DocumentCapability.DOCUMENT_TO_PDF, DocumentCapability.DOCUMENT_TO_TEXT)
            .map(
                c -> {
                  val s = new CapabilityStatusRecord();
                  s.setCapability(c);
                  return s;
                })
            .collect(Collectors.toList());

Thanks!

waiting-for-feedback

Most helpful comment

I'm seeing the aformentioned behavior in 1.18.4. (using gradle/intellij (with latest plugin circa 2018-12-01))

-Surprisingly its happening within a 'try' as opposed to a lamda-

Looks like this might be a red herring. The root of the issue was:

val future = CompletableFuture.supplyAsync(() -> CompletableFuture.completedFuture(1));
try {
  val result = future.get();
...

explicitly defining the generic type solved the problem:
val future = CompletableFuture.<Integer>supplyAsync(() -> CompletableFuture.completedFuture(1));

All 3 comments

Hello @dodalovic ,
do you still have this problem with val? What are your steps to reproduce this behavior?

The error text "java: Cannot use 'val' here because initializer expression does not have a representable type: Type cannot be resolved" comes from lombok.jar, not from lombok-plugin.
Similar example: https://github.com/rzwitserloot/lombok/issues/729

I can only reproduce it, if I make a mvn clean first and try to recompile only the class with "val in lamda" after that.
But if I compile with maven or build entire project in IntelliJ, everything is fine.
val-processing needs all other classes to be already compiled, before processing references to it. So it would be correct behavior.

I'm seeing the aformentioned behavior in 1.18.4. (using gradle/intellij (with latest plugin circa 2018-12-01))

-Surprisingly its happening within a 'try' as opposed to a lamda-

Looks like this might be a red herring. The root of the issue was:

val future = CompletableFuture.supplyAsync(() -> CompletableFuture.completedFuture(1));
try {
  val result = future.get();
...

explicitly defining the generic type solved the problem:
val future = CompletableFuture.<Integer>supplyAsync(() -> CompletableFuture.completedFuture(1));

I see this when combining val with another example, where there is an generic return type.

  public static <T> T or(T object, T ifNull) {
    return object != null ? object : ifNull;
  } 
   String x = "b";
   val y = x.or("a");
   val z = ObjectExtensions.or(x, "a");

Delombok infers Object instead of String:

    String x = "b";
    final java.lang.Object y = x.or("a");
    final java.lang.Object z = ObjectExtensions.or(x, "a");
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Nissaum picture Nissaum  路  3Comments

gigaSproule picture gigaSproule  路  4Comments

inthink picture inthink  路  5Comments

timothymdavis picture timothymdavis  路  4Comments

eilensm picture eilensm  路  6Comments