Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Default Value Support in Lombok #3664

Open
lyf2nb opened this issue May 7, 2024 · 2 comments
Open

[FEATURE] Default Value Support in Lombok #3664

lyf2nb opened this issue May 7, 2024 · 2 comments

Comments

@lyf2nb
Copy link

lyf2nb commented May 7, 2024

Feature Request: Default Value Support in Lombok

Describe the feature
A clear and concise description of the feature request is to add support for setting default values in Lombok-generated getters. The goal of this feature request is to allow developers to specify default values for fields, ensuring that getters return these default values when the field is null.

Java Snippets:

import lombok.Getter;

public class MyClass {
    @Getter(defaultValue = BigDecimal.ZERO)
    private BigDecimal value; // Desired feature: Ability to specify default value, e.g., default = BigDecimal.ZERO
}
public class MyClass {
    private BigDecimal value;

    public BigDecimal getValue() {
        return value== null ? BigDecimal.ZERO : value;
    }
}

Describe the target audience
This feature proposal would benefit developers who use Lombok to reduce boilerplate code in their Java projects. It would particularly help those who need to ensure that getters return default values when the field is null.

Additional context
Adding support for default values in Lombok-generated getters would enhance the flexibility and convenience of using Lombok annotations, making it easier for developers to handle default values without manually writing getter methods. This feature would streamline the development process and improve code readability in projects utilizing Lombok.

Can I Implement This Feature Myself?
Certainly, you can implement this feature yourself by creating custom getter methods in your classes to handle default values. This approach provides flexibility in setting default values based on your specific needs and allows you to tailor the implementation to suit your requirements.

@sixcorners
Copy link

Why not use a field initializer?

@lyf2nb
Copy link
Author

lyf2nb commented May 22, 2024

Why not use a field initializer?

Because you can set it to null, and if you query the database, it will return null

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants