Then icon indicating copy to clipboard operation
Then copied to clipboard

[proposal] Mutating a value type

Open dtrofimov opened this issue 4 years ago • 1 comments

Motivation

To mutate some deeply nested value types, we have to write the whole key path at least twice:

some.very[deep].structure.someProperty = someValue
some.very[deep].structure.anotherProperty = anotherValue

// or with Then:

some.very[deep].structure = some.very[deep].structure.with {
    $0.someProperty = someValue
    $0.anotherProperty = anotherValue
}

Solution

Let's extend Then with mutate function (alternatives: update, access, write):

extension Then {
    public mutating func mutate(_ block: (inout Self) throws -> Void) rethrows {
        try block(&self)
    }
}

Usage:

some.very[deep].structure.mutate {
    $0.someProperty = someValue
    $0.anotherProperty = anotherValue
}

It would be great to have this in Then library. Thank you!

dtrofimov avatar Apr 27 '21 20:04 dtrofimov

Hope to be a PR.

tanpengsccd avatar Mar 24 '22 01:03 tanpengsccd