auto-matter icon indicating copy to clipboard operation
auto-matter copied to clipboard

Adding an annotation to set default value

Open honnix opened this issue 8 years ago • 9 comments

Would something like this make sense?

@AutoMatter
public interface Room {
  @Default(value = "foo")
  String id();
}

honnix avatar Nov 14 '17 09:11 honnix

So something like?

...
  static RoomBuilder builder() {
    return new RoomBuilder().id("foo");
  }
...

honnix avatar Nov 14 '17 09:11 honnix

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";
  }
}

danielnorberg avatar Nov 14 '17 12:11 danielnorberg

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 avatar Nov 14 '17 14:11 honnix

@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.

danielnorberg avatar Nov 15 '17 02:11 danielnorberg

See https://immutables.github.io/immutable.html#default-attributes

Immutables btw is a great lib that already does all of this. Try it out =)

danielnorberg avatar Nov 15 '17 02:11 danielnorberg

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

honnix avatar Nov 15 '17 06:11 honnix

👍

bkuberek avatar Jan 31 '18 20:01 bkuberek

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;
  }
}

danielnorberg avatar Oct 23 '18 01:10 danielnorberg

https://github.com/danielnorberg/auto-matter/pull/66

danielnorberg avatar Oct 23 '18 03:10 danielnorberg