Skip to content

Instantly share code, notes, and snippets.

@piotrtobolski
Forked from krzyzanowskim/with.swift
Last active May 20, 2019 16:12
Show Gist options
  • Save piotrtobolski/6c964de93390fc508b9e75c974f19a1b to your computer and use it in GitHub Desktop.
Save piotrtobolski/6c964de93390fc508b9e75c974f19a1b to your computer and use it in GitHub Desktop.
@discardableResult
@inlinable
private func with<T>(_ value: T, _ builder: (T) throws -> Void) rethrows -> T {
try builder(value)
return value
}
// Use
class MyClass {
var prop = "it's"
init() {}
}
let instance = with(MyClass()) {
$0.prop += " awesome"
}
print(instance.prop) //"it's awesome"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment