Skip to content

Instantly share code, notes, and snippets.

@fmo91
Created July 6, 2020 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fmo91/59f5764727ef782476c27ba5dba54766 to your computer and use it in GitHub Desktop.
Save fmo91/59f5764727ef782476c27ba5dba54766 to your computer and use it in GitHub Desktop.
Value types mutation on copy operator
import Foundation
precedencegroup MutationPrecedence {
associativity: left
}
infix operator +> : MutationPrecedence
func +> <A> (v: A, f: (inout A) -> Void) -> A {
var copy = v
f(&copy)
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