Skip to content

Instantly share code, notes, and snippets.

@preble
Created April 11, 2016 18:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save preble/a5fc0d3433cc83facadea48ed314c102 to your computer and use it in GitHub Desktop.
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?
protocol Mutatable {}
extension Mutatable {
func mutated(f: (inout Self) -> Void) -> Self {
var copy = self
f(&copy)
return copy
}
}
/*
Example usage:
extension CGPoint: Mutatable {}
let p = CGPoint.zero.mutated { $0.x = 32 }
*/
@preble
Copy link
Author

preble commented Apr 11, 2016

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