Created
November 8, 2015 01:23
-
-
Save curtclifton/d52c59bb433a80bcd16b to your computer and use it in GitHub Desktop.
This file contains 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
//: 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