[ 
https://issues.apache.org/jira/browse/GROOVY-12015?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Paul King updated GROOVY-12015:
-------------------------------
    Description: 
h3. Summary

Extends {{copyWith}} (generated for {{@Immutable}} and {{@RecordType}}
classes declared with {{{}copyWith=true{}}}) with nested-path support and a
transactional block form, for ergonomic updates of immutable object graphs.
h3. Nested-path map form

A {{copyWith}} key may now be a dotted path; the affected nested nodes are
rebuilt recursively and untouched branches are reused.
{code:groovy}
def q = p.copyWith('address.city': 'NYC', 'address.zip': '10001')
{code}
 * Works for arbitrarily deep paths and heterogeneous {{{}@Immutable{}}}/
{{@RecordType}} graphs (record-in-record included).
 * Plain (non-dotted) keys behave exactly as before.
 * Identity is preserved transitively: if nothing changes, the original
instance is returned.
 * Every node on a nested path must itself provide {{{}copyWith(Map){}}};
otherwise a clear, specific error is raised.

h3. Transactional block form

A {{copyWith(Closure)}} overload is also generated:
{code:groovy}
def q = p.copyWith {
    name = 'Bob'                                 // plain set
    address.city = old.address.city.reverse()    // derive from the original
    fullName = old.first + ' ' + old.last        // cross-field derivation
    loginCount.modify { it + 1 }                 // single-field shorthand
}
{code}
 * {{old}} resolves to the original (pre-state) object, consistent with
{{old}} in {{{}@Ensures{}}}/{{{}@Contract{}}}, and may be used to derive any new
value, including across fields.
 * {{prop.modify\{ \} }} is a shorthand for the common 
transform-this-same-field case.
 * To invoke a real same-named {{modify}} method on a value type (or simply for 
readability), use the {{old}} form: x = old.x.modify { ... \}.
 * An empty or no-op block returns the original instance.

h3. Compatibility
 * A no-arg {{copyWith()}} is generated, returning the same instance
(preserving the historical zero-argument behaviour).
 * Existing single-level and Map-based {{copyWith}} usage is unchanged.
 * Shared generation logic is used by both the {{@Immutable}} and
{{@RecordType}} transforms so behaviour stays identical across the two.

  was:
h3. Summary

Extends {{copyWith}} (generated for {{@Immutable}} and {{@RecordType}}
classes declared with {{copyWith=true}}) with nested-path support and a
transactional block form, for ergonomic updates of immutable object graphs.

h3. Nested-path map form

A {{copyWith}} key may now be a dotted path; the affected nested nodes are
rebuilt recursively and untouched branches are reused.

{code:groovy}
def q = p.copyWith('address.city': 'NYC', 'address.zip': '10001')
{code}

* Works for arbitrarily deep paths and heterogeneous {{@Immutable}}/
  {{@RecordType}} graphs (record-in-record included).
* Plain (non-dotted) keys behave exactly as before.
* Identity is preserved transitively: if nothing changes, the original
  instance is returned.
* Every node on a nested path must itself provide {{copyWith(Map)}};
  otherwise a clear, specific error is raised.

h3. Transactional block form

A {{copyWith(Closure)}} overload is also generated:

{code:groovy}
def q = p.copyWith {
    name = 'Bob'                                 // plain set
    address.city = old.address.city.reverse()    // derive from the original
    fullName = old.first + ' ' + old.last        // cross-field derivation
    loginCount.modify { it + 1 }                 // single-field shorthand
}
{code}

* {{old}} resolves to the original (pre-state) object, consistent with
  {{old}} in {{@Ensures}}/{{@Contract}}, and may be used to derive any new
  value, including across fields.
* {{prop.modify { }}} is a shorthand for the common
  transform-this-same-field case.
* To invoke a real same-named {{modify}} method on a value type (or simply
  for readability), use the {{old}} form: {{x = old.x.modify { ... }}}.
* An empty or no-op block returns the original instance.

h3. Compatibility

* A no-arg {{copyWith()}} is generated, returning the same instance
  (preserving the historical zero-argument behaviour).
* Existing single-level and Map-based {{copyWith}} usage is unchanged.
* Shared generation logic is used by both the {{@Immutable}} and
  {{@RecordType}} transforms so behaviour stays identical across the two.



> Provide a nested copyWith capability
> ------------------------------------
>
>                 Key: GROOVY-12015
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12015
>             Project: Groovy
>          Issue Type: New Feature
>            Reporter: Paul King
>            Assignee: Paul King
>            Priority: Major
>
> h3. Summary
> Extends {{copyWith}} (generated for {{@Immutable}} and {{@RecordType}}
> classes declared with {{{}copyWith=true{}}}) with nested-path support and a
> transactional block form, for ergonomic updates of immutable object graphs.
> h3. Nested-path map form
> A {{copyWith}} key may now be a dotted path; the affected nested nodes are
> rebuilt recursively and untouched branches are reused.
> {code:groovy}
> def q = p.copyWith('address.city': 'NYC', 'address.zip': '10001')
> {code}
>  * Works for arbitrarily deep paths and heterogeneous {{{}@Immutable{}}}/
> {{@RecordType}} graphs (record-in-record included).
>  * Plain (non-dotted) keys behave exactly as before.
>  * Identity is preserved transitively: if nothing changes, the original
> instance is returned.
>  * Every node on a nested path must itself provide {{{}copyWith(Map){}}};
> otherwise a clear, specific error is raised.
> h3. Transactional block form
> A {{copyWith(Closure)}} overload is also generated:
> {code:groovy}
> def q = p.copyWith {
>     name = 'Bob'                                 // plain set
>     address.city = old.address.city.reverse()    // derive from the original
>     fullName = old.first + ' ' + old.last        // cross-field derivation
>     loginCount.modify { it + 1 }                 // single-field shorthand
> }
> {code}
>  * {{old}} resolves to the original (pre-state) object, consistent with
> {{old}} in {{{}@Ensures{}}}/{{{}@Contract{}}}, and may be used to derive any 
> new
> value, including across fields.
>  * {{prop.modify\{ \} }} is a shorthand for the common 
> transform-this-same-field case.
>  * To invoke a real same-named {{modify}} method on a value type (or simply 
> for readability), use the {{old}} form: x = old.x.modify { ... \}.
>  * An empty or no-op block returns the original instance.
> h3. Compatibility
>  * A no-arg {{copyWith()}} is generated, returning the same instance
> (preserving the historical zero-argument behaviour).
>  * Existing single-level and Map-based {{copyWith}} usage is unchanged.
>  * Shared generation logic is used by both the {{@Immutable}} and
> {{@RecordType}} transforms so behaviour stays identical across the two.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to