Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I guess the obvious choices are:</p> <p>o Switch to a transient data structure (builder) for the update. This is quite normal. <code>StringBuilder</code> for <code>String</code> manipulation for example. As your example.</p> <pre><code>Person p3 = Builder.update(p) .withAddress( Builder.update(p.address()) .withCity("Berlin") .build() ) .build(); </code></pre> <p>o Always use persistent structures. Although there appears to be lots of copying, you should actually be sharing almost all state, so it is nowhere near as bad as it looks.</p> <pre><code>final Person p3 = p .withAddress( p.address().withCity("Berlin") ); </code></pre> <p>o Explode the data structure into lots of variables and recombine with one huge and confusing constructor.</p> <pre><code>final Person p3 = Person.of( p.name(), Address.of( p.house(), p.street(), "Berlin", p.country() ), p.x(), p.y(), p.z() ); </code></pre> <p>o Use call back interfaces to provide the new data. Even more boilerplate.</p> <pre><code>final Person p3 = Person.of(new PersonInfo( public String name () { return p.name(); ) public Address address() { return Address.of(new AddressInfo() { private final Address a = p.address(); public String house () { return a.house() ; } public String street () { return a.street() ; } public String city () { return "Berlin" ; } public String country() { return a.country(); } })), public Xxx x() { return p.x(); } public Yyy y() { return p.y(); } public Zzz z() { return p.z(); } }); </code></pre> <p>o Use nasty hacks to make fields transiently available to code.</p> <pre><code>final Person p3 = new PersonExploder(p) {{ a = new AddressExploder(a) {{ city = "Berlin"; }}.get(); }}.get(); </code></pre> <p>(Funnily enough I was just put down a copy of Purely Functional Data Structures by Chris Okasaki.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload