Created
April 11, 2016 18:23
-
-
Save preble/a5fc0d3433cc83facadea48ed314c102 to your computer and use it in GitHub Desktop.
Need to mutate a value type but don't want to create a var in your scope?
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 Mutatable {} | |
extension Mutatable { | |
func mutated(f: (inout Self) -> Void) -> Self { | |
var copy = self | |
f(©) | |
return copy | |
} | |
} | |
/* | |
Example usage: | |
extension CGPoint: Mutatable {} | |
let p = CGPoint.zero.mutated { $0.x = 32 } | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This technique can also be seen here: https://github.com/devxoul/Then/blob/master/Sources/Then.swift#L25-L42