[FEATURE] Optional Parameters
I hope this is not a duplicate, but I could not find anything similar. It would be great if Lombok could be used to generate different versions of a method with and without optional parameters. For this, a new annotation @optional(default) could be introduced that can be added to parameters of methods (or constructors). For example, consider the "lomboked" method:
public void doSomething(String a, @optional(42) int b, @optional(false) boolean c) {
// method body
}The default value itself could be optional, too, resulting in defaults like 0, false, or null depending on the type of the parameter. This would result in 4 different versions of the method (in general: 2^(number of @optional) versions):
public void doSomething(String a) {
doSomething(a, 42, false);
}
public void doSomething(String a, int b) {
doSomething(a, b, false);
}
public void doSomething(String a, boolean c) {
doSomething(a, 42, b);
}
public void doSomething(String a, int b, boolean c) {
// method body
}Whereas the following would result in an error and thus would not be allowed, as there are two optional parameters of the same type (without a non-optional parameter in between) so the different methods would have the same signature:
public void doesNotWork(A a, @optional(42) int b, @optional(23) int c) {
Optionally, optional parameters could also be paired or grouped. E.g. in the following example, there would only be two versions of the method: One with all parameters, and one with only the required parameters, but none with the individual optional parameters. (This would also allow for multiple optional parameters of the same type in a row.)
public void doSomething(String a, @optional(42, group=0) int b, @optional(false, group=0) boolean c) {
// method body
}Often Java code contains many versions of the same methods (or constructors) with different parameters, some of which are optional. Usually, there is one "full" version of the method that gets called by all the others with appropriate default values. With the @optional(defaultValue) annotation this would not be needed any more.
Source: projectlombok/lombok