Lombok.val allows
use val as the declaration type of the local variable instead of actually writing the type. When you do this, the type will be inferred from the initializer expression. The local variable will also be made final.
So instead
final ArrayList<String> example = new ArrayList<String>();
You can write
val example = new ArrayList<String>();
I tried to do some research on how this works, but there doesn't seem to be a huge amount of information. Looking at the github page , I see that val
is a type of annotation. Then the annotation type is used, not the actual annotation.
I did not even suspect that you could use the types of annotations in this way, but when testing it really works. However, I'm still not sure why you want to use this type in this way.
public class Main { public @interface Foo { } public static void main(String... args) { Foo bar; System.out.println("End"); } }
How does Lombok handle these practices if they are not annotations, but types of annotations? For my (obviously wrong) understanding, the syntax should look more:
@Val foo = new ArrayList<String>();
(I know that annotation restrictions mean that the syntax above is invalid)
source share