Skip to content

Instantly share code, notes, and snippets.

@curtclifton
Created November 8, 2015 01:23
Show Gist options
  • Save curtclifton/d52c59bb433a80bcd16b to your computer and use it in GitHub Desktop.
Save curtclifton/d52c59bb433a80bcd16b to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
struct Foo {
private(set) var bar: String
let baz: String
mutating func updateWith(bar: String) {
self.bar = bar
}
mutating func updateAfter(bar: String) -> () -> Foo {
return {
// Seems like self here is a copy, even though I thought closures capture by reference? Is there anyway to have self here be a reference so that the 'original' gets updated, without making Foo a class?
self.bar = bar
return self
}
}
}
var i = Foo(bar: "a", baz: "cat")
print(i) // "Foo(bar: "a", baz: "cat")
i.updateWith("b")
print(i) // "Foo(bar: "b", baz: "cat")
let j = i.updateAfter("c")
print(i) // "Foo(bar: "b", baz: "cat")
i = j()
print(i) // "Foo(bar: "c", baz: "cat")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment