Skip to content

Instantly share code, notes, and snippets.

@PaulTaykalo
Created November 10, 2017 14:33
Show Gist options
  • Save PaulTaykalo/43ff20e3313644c5b3ab7390221e8e7a to your computer and use it in GitHub Desktop.
Save PaulTaykalo/43ff20e3313644c5b3ab7390221e8e7a to your computer and use it in GitHub Desktop.
Mutable struct via block in swift
protocol Updateable {
func updated(block: (inout Self) -> ()) -> Self
}
extension Updateable {
func updated(block: (inout Self) -> ()) -> Self {
var item = self
block(&item)
return item
}
}
extension Activity: Updateable { }
struct Activity {
var a: String
var b: String
}
let p = Activity(a: "a", b:"b")
let c = p.updated {
$0.a = "12"
$0.b = "13"
}
print("\(c)")
@eastari
Copy link

eastari commented Nov 12, 2017

Is it - instead of the lens? Cool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment