Created
July 6, 2020 00:15
-
-
Save fmo91/59f5764727ef782476c27ba5dba54766 to your computer and use it in GitHub Desktop.
Value types mutation on copy operator
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
import Foundation | |
precedencegroup MutationPrecedence { | |
associativity: left | |
} | |
infix operator +> : MutationPrecedence | |
func +> <A> (v: A, f: (inout A) -> Void) -> A { | |
var copy = v | |
f(©) | |
return copy | |
} | |
struct User { | |
var name: String | |
var occupation: String | |
} | |
let fede = User(name: "Fede", occupation: "Developer") | |
let fer = fede +> { $0.name = "Fer" } | |
print(fede) | |
print(fer) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment