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

field default values #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

field default values #66

wants to merge 1 commit into from

Conversation

danielnorberg
Copy link
Owner

@AutoMatter
interface Foobar {

  String baz();

  @AutoMatter.Field
  default String foo() {
    return "foo";
  }

  @AutoMatter.Field
  default String bar() {
    // Default valued fields can refer to other fields
    // Note: The fields referred to must be _above_ the referring field.
    return foo() + " bar " + baz();
  }
}

// ...

Foobar foobarWithFooDefault = new FoobarBuilder()
    .baz("baz")
    .build();

// prints: Foobar{baz=baz, foo=foo, bar=foo bar baz}
out.println("default values only: " + foobarWithFooDefault);

Foobar foobarWithFooValue = new FoobarBuilder()
    .baz("baz")
    .foo("hello")
    .build();

// prints: Foobar{baz=baz, foo=hello, bar=hello bar baz}
out.println("with foo value: " + foobarWithFooValue);

```java
@automatter
interface Foobar {

  String baz();

  @AutoMatter.Field
  default String foo() {
    return "foo";
  }

  @AutoMatter.Field
  default String bar() {
    // Default valued fields can refer to other fields
    // Note: The fields referred to must be _above_ the referring field.
    return foo() + " bar " + baz();
  }
}

// ...

Foobar foobarWithFooDefault = new FoobarBuilder()
    .baz("baz")
    .build();

// prints: Foobar{baz=baz, foo=foo, bar=foo bar baz}
out.println("default values only: " + foobarWithFooDefault);

Foobar foobarWithFooValue = new FoobarBuilder()
    .baz("baz")
    .foo("hello")
    .build();

// prints: Foobar{baz=baz, foo=hello, bar=hello bar baz}
out.println("with foo value: " + foobarWithFooValue);
```
@danielnorberg
Copy link
Owner Author

TODO:

  • Allow Optional fields?
  • Allow collection/map fields?
  • Add @AutoMatter.Default annotation instead of using @AutoMatter.Field?

@danielnorberg
Copy link
Owner Author

Note: does not work well with gson as it bypasses the value constructor.

@DLisk
Copy link

DLisk commented Feb 28, 2020

@danielnorberg Are there plans to merge this into master sometime soon?

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

Successfully merging this pull request may close these issues.

2 participants