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

Adding an annotation to set default value #56

Open
honnix opened this issue Nov 14, 2017 · 9 comments
Open

Adding an annotation to set default value #56

honnix opened this issue Nov 14, 2017 · 9 comments

Comments

@honnix
Copy link
Contributor

honnix commented Nov 14, 2017

Would something like this make sense?

@AutoMatter
public interface Room {
  @Default(value = "foo")
  String id();
}
@honnix
Copy link
Contributor Author

honnix commented Nov 14, 2017

So something like?

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

@honnix honnix closed this as completed Nov 14, 2017
@danielnorberg
Copy link
Owner

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

@honnix
Copy link
Contributor Author

honnix commented Nov 14, 2017

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 honnix reopened this Nov 14, 2017
@danielnorberg
Copy link
Owner

danielnorberg commented Nov 15, 2017

@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
Copy link
Owner

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

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

@honnix
Copy link
Contributor Author

honnix commented Nov 15, 2017

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

@bkuberek
Copy link

👍

@danielnorberg
Copy link
Owner

danielnorberg commented Oct 23, 2018

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
Copy link
Owner

#66

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

3 participants