Show HN: Toward Nirvana with Optional Parameters
Oracle refuses to add optional parameters (and named args) to Java. So I did via a javac plugin and a smidge of hacking to modify the AST. The result is a pretty comprehensive implementation of the feature, without breaking binary compatibility. Here's a short summary.
---
The manifold-params compiler plugin adds support for optional parameters and named arguments in Java methods, constructors, and records -- offering a simpler, more expressive alternative to method overloading and builder patterns.
public String valueOf(char[] data,
int offset = 0,
int count = data.length - offset) {...}
valueOf(array) // use defaults for offset and count
valueOf(array, 2) // use default for count
valueOf(array, count:20) // use default for offset by naming count
This plugin supports JDK versions 8 - 21+ and integrates seamlessly with IntelliJ IDEA and Android Studio.Key features:
- Optional parameters -- Define default values directly in methods, constructors, and records
- Named arguments -- Call methods using parameter names for clarity and flexibility
- Flexible defaults -- Use expressions, reference earlier parameters, and access local methods and fields
- Customizable behavior -- Override default values in subclasses or other contexts
- Safe API evolution -- Add parameters or change defaults without breaking binary or source compatibility
- Eliminates overloads and builders -- Collapse boilerplate into a single, expressive method or constructor
- IDE-friendly -- Fully supported in IntelliJ IDEA and Android Studio
Learn more: https://github.com/manifold-systems/manifold/blob/master/man...
No comments yet