immutables icon indicating copy to clipboard operation
immutables copied to clipboard

Default value annotation?

Open micopiira opened this issue 4 years ago • 2 comments

I was wondering if it would be possible to implement setting default value for a property using an annotation?

It would be a bit cleaner IMO. Something like:

@Value.Immutable
public interface Interface {
  @Value.Default("default")
  String someString();
}

vs

@Value.Immutable
public interface Interface {
  default String someString() {
    return "default";
  }
}

The annotation processor could probably check that the value passed to the annotation is the same type as the return type?

micopiira avatar Sep 24 '21 18:09 micopiira

I understand we're saving a couple of lines here but are there other benefits in having the expression not in the method body ?

BTW @Value.Default("foo" + 33) is also a valid annotation expression.

asereda-gs avatar Sep 29 '21 02:09 asereda-gs

@asereda-gs I can't think of any other benefits than saving a couple of lines. But one significant difference in behaviour is when using a default annotation, any manual implementations of the interface do not inherit the default value, which may be desired in some circumstances.

micopiira avatar Sep 29 '21 10:09 micopiira