opened
MFlisar
username_0
When using `Memoized` and builders in common, there does not seem a way to copy the memoized data from one object to another.

Example:

    Data d = Data.create();
    d.getHeavyData(); // this field is annotated with @Memoized
    Data copy = d.toBuilder().build();
    copy.getHeavyData(); // this will execute the heavy calculation again

Therefore I would need an (optional at best) possibility to copy the memoized data to the new object. Can this be done? If yes, how? If no, I think it would make sense to add this.
Memoized - copy data to new object
issue
created
netdpb
username_1
Hmm. `@Memoized` is intended for properties that are derived from other immutable properties on the same object. If you were able to copy that property onto a new object, that would invalidate that invariant, wouldn't it? What's to stop you from copying the wrong value?

If your `@Memoized` property is derived from only a subset of the properties of your object, maybe you could consider extracting out the `@Memoized` property and those it depends on into a new `@AutoValue` object, which itself could become a property:

Instead of:

```
@AutoValue abstract class Foo {
  abstract A a();
  abstract B b();
  abstract C c();
  @Memoized D d() {
    return someFunctionOf(a(), b());
  }
}
```

you could do:

```
@AutoValue abstract class Foo {
  abstract Bar bar();
  abstract C c();
}

@AutoValue abstract class Bar {
  abstract A a();
  abstract B b();
  @Memoized D d() {
    return someFunctionOf(a(), b());
  }
}
```

Of course, you could provide convenience methods to get `A`, `B`, and `D` directly from an instance of `Foo` if that helps.
comment
created
MFlisar
username_0
That looks fine, this way I can hand on the Bar object to a new Foo object.

And I see the problem now with exposing a setter for a builder, this could lead to inconsistent states in the new object...
comment
created
netdpb
username_1
Right. I'm going to close this issue for now. Feel free to reopen if you have more thoughts on it.
comment
closed
netdpb
username_1
issue