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

[BUG] @lombok.Builder.Default does not work on records #3547

Open
victorherraiz opened this issue Nov 14, 2023 · 2 comments
Open

[BUG] @lombok.Builder.Default does not work on records #3547

victorherraiz opened this issue Nov 14, 2023 · 2 comments

Comments

@victorherraiz
Copy link

Applying @Defaultto a record class fails to compile.

@lombok.Builder
record Foo (
  // Fails to compile.
  @lombok.Builder.Default String bar = "ok"
) {}

The expected behaviour is that works as in a normal class

  • Lombok version: 1.18.30
  • Platform: Java 17, IntelliJ IDEA 2023.2.5 (Ultimate Edition)
@mjustin
Copy link

mjustin commented Dec 6, 2023

Per Lombok author @rzwitserloot's comment on a relevant Stack Overflow question about whether this is possible:

Not right now, I hadn't considered that when extending support of @Builder to records. Unfortunately, I cannot think off of the top of my head of some simple solution to this problem - you can't stick arbitrary expressions in annotations, so e.g. @Builder.Default(System.getProperty("user.home")) doesn't compile - lombok can't do it that way.

Also:

if we do this, probably make you write a static method that produces the value and takes no args, and annotate it with something. For example @Builder.Default, and the name has to be defaultX; if it isn't in that format, you get an error on the annotation explaining how to name them.

An answer to that question calls out a different approach if default values are needed, which is to customize the builder class to set the default value, instead of using @Builder.Default:

@lombok.Builder
record Foo(String bar) {
  public static class FooBuilder {
    private String bar = "ok";
  }
}

@mjustin
Copy link

mjustin commented Dec 6, 2023

Incidentally, as long as @Default on records is officially unsupported, it would be nice to see that in the @Builder documentation.

kraiss added a commit to kraiss/lombok that referenced this issue Mar 29, 2024
kraiss added a commit to kraiss/lombok that referenced this issue Mar 29, 2024
kraiss added a commit to kraiss/lombok that referenced this issue Mar 29, 2024
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