Created
November 10, 2017 14:33
-
-
Save PaulTaykalo/43ff20e3313644c5b3ab7390221e8e7a to your computer and use it in GitHub Desktop.
Mutable struct via block in swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is it - instead of the lens? Cool.