auto-matter
auto-matter copied to clipboard
Adding an annotation to set default value
Would something like this make sense?
@AutoMatter
public interface Room {
@Default(value = "foo")
String id();
}
So something like?
...
static RoomBuilder builder() {
return new RoomBuilder().id("foo");
}
...
Could be interesting to make automatter support something like this:
@AutoMatter
public interface Room {
// default method called if value not set
default String id() {
return "foo";
}
}
So if I understood correctly.
@AutoMatter
public interface Room {
String id();
// default method called if value not set
@DefaultValueOf(property = "id")
default String defaultId() {
return "foo";
}
}
Automatter can search for default method with annotation for the property?
@honnix I mean just make the id() method a java 8 default method and have automatter call it to get the default value if id is not set. No need for a separate method.
See https://immutables.github.io/immutable.html#default-attributes
Immutables btw is a great lib that already does all of this. Try it out =)
That lib looks very capable!
Yeah we don’t need a separated method. Having an annotation telling automatter that this is a property default method instead of any other user defined default method would still make sense I think. Otherwise it is implicit and there is an ambiguity. Kind of like @Value.Default
👍
I'm currently working on this:
@AutoMatter
interface DefaultValueExample {
// Normally auto-matter ignores default methods, so the @AutoMatter.Field
// annotation instructs it to treat the method as a default value provider.
@AutoMatter.Field
default String foo() {
return "foo";
}
@AutoMatter.Field
default int bar() {
return 17;
}
}
https://github.com/danielnorberg/auto-matter/pull/66