1

I'm integrating Project Lombok in our Android app. I'm running into issues with using val and var. I'm getting Incompatible Types error for a field of type var or val, when I assign a value (of any type) to it.

private final val example = new ArrayList<String>();

private var eg2;
eg2=getRandomString();

Has anyone tried var and val in Android? Any help would be appreciated.

I'm using Lombok version 1.16.12 ,Android Studio 2.3 Beta 1, Android Plugin version 2.2.3, and Gradle 3.2.1.

NullPointer
  • 496
  • 6
  • 14
  • 1
    Does it compile without IntelliJ just using Gradle? Have you enabled annotation processing for the project in IntelliJ? (Settings -> Compiler -> Annotation Processors -> Enable annotation processing) Are you using https://plugins.jetbrains.com/idea/plugin/6317-lombok-plugin ? – Joseph Earl Dec 28 '16 at 19:30
  • No, it doesn't compile just using Gradle either. Yes, I have enabled annotation processing and have the lombok plugin installed. Everything else works like a charm `@Data`, `@Value`,` @Getter` etc, it's just val and var that I'm having trouble with. – NullPointer Dec 28 '16 at 19:35
  • Seems like you have it all set up then. – Joseph Earl Dec 28 '16 at 19:45
  • 1
    Your var declaration `private var eg2; eg2=getRandomString();` wont work - the assignment needs to be done in the same statement so that lombok can figure out the type - `private var eg2=getRandomString();`. – Joseph Earl Dec 28 '16 at 19:45
  • I'm not sure whether val works if you have final before it since val automatically includes that itself, does it work if you remove the final from there? – Joseph Earl Dec 28 '16 at 19:46
  • `private var eg2=getRandomString();` did not work either. `var` didn't work in the field or the local variable. But `val` worked, when used as a local variable instead of a field. – NullPointer Dec 28 '16 at 19:57
  • The `final` change didn't help either. – NullPointer Dec 28 '16 at 19:58

1 Answers1

2

I think I can see your issue - @val and @var only work for local variables - that is variables within a method or block.

So the following will work:

public class ValExample {
  public String example() {
    val example = new ArrayList<String>();
    example.add("Hello, World!");
    val foo = example.get(0);
    return foo.toLowerCase();
  }
}

But it won't work with class members (this won't build):

public class BadValExample {
  private val example = new ArrayList<String>();
}

@var also needs to be manually enabled. To do this add a lombok.config to your project with the contents:

lombok.var.flagUsage = ALLOW
Joseph Earl
  • 23,351
  • 11
  • 76
  • 89
  • I see. How about var? What's the right way to use var? – NullPointer Dec 28 '16 at 19:59
  • Actually I can't see @var listed as supported on the IntelliJ plugin https://plugins.jetbrains.com/idea/plugin/6317-lombok-plugin. There's a GitHub issue to add support to it https://github.com/mplushnikov/lombok-intellij-plugin/issues/322. In that case var may work if you build using Gradle. – Joseph Earl Dec 28 '16 at 20:04
  • Cool! Thank you so much for the help! – NullPointer Dec 28 '16 at 20:09
  • 1
    It looks like it might be disabled by default actually - https://github.com/rzwitserloot/lombok/blob/192bfe769306db9b7f9cc7091a1f6f8cddf06899/doc/changelog.markdown (see v1.16.12) Add a lombok.config to your project with the line `lombok.var.flagUsage = ALLOW` and it should work in Gradle, although maybe not IntelliJ still. – Joseph Earl Dec 28 '16 at 20:10