Support for NEW @Default annotation on method Parameters

38 views
Skip to first unread message

Murali Aakula

unread,
Jun 18, 2021, 12:51:41 PM6/18/21
to Project Lombok
Hi,

Thank you so much for THE BEST library.

I would like to propose a new annotation @Default (or something better) for Method Parameters. Must support for static Methods and in interface too.

Ex:
Lombok:
Class MyUtility {
public void showMessage(@Default("No Records Found." String msg) {
...
}
}


Vanilla:
Class MyUtility {
public void showMessage() {
showMessage(null);
}
public void showMessage(String msg) {
if (msg == null) {
msg = "No Records Found.";
}
...
}
}

Thanks in advance.

Regards,
Murali.

Reinier Zwitserloot

unread,
Jun 18, 2021, 12:57:41 PM6/18/21
to Project Lombok
We really cant. The problem is, annotations are restricted; the annotation definition itself requires that you state precisely what the type is of any given parameter. Thus, your proposed `@Default` annotation would be a thing you can either:

* Only apply to String variables, so we really need `@DefaultInt`, `@DefaultDouble`, `@DefaultString`, etc, and we flat out cannot support non-primitive, non-string, non-array-of-those as annotations simply won't allow it. This is problematic; for example, it would be weird if you can't provide a default for e.g. `java.time.LocalDate`.
* You write a value in a string and lombok parses and runs it. Effectively you now have java code in string form in a java source file which is a step we find sufficiently ugly and inpalatable that we just shoot such ideas down. I don't want `@Default("LocalDate.of(2021, 1, 1)")` to ever be a thing.
* Require some fairly major hackery in java internals that require agent-style injection (though, now that we know about `-Xplugin`, maaayyybe one day this is part of the allowable lombok syntax batch. It's not yet, though), because if you stick a non-valid (as in, wrong type) value in an annotation value, then javac will just insta-bomb out with a compile time error and never even invoke lombok. We don't even get to run to set things right. So, `@Default(LocalDate.of(2021, 1, 1))` cannot work. Even if we do the deep dive and 'fix' it with agent-style injection, IDEs and the like flat out will refuse to cooperate and e.g. stop showing autocomplete options because IDEs think (correctly for all annotations except, in this hypothetical situation, lombok annotations) that you can't put localdates in an annotation, ever.

Combine those things and this is a huge undertaking with a ton of caveats; far too many.

We have brainstormed about default parameters and have plenty of ideas. It just wouldn't look like that.



Reply all
Reply to author
Forward
0 new messages