Created
July 27, 2015 23:19
-
-
Save yashigani/b75c21ebff9f6432d1c1 to your computer and use it in GitHub Desktop.
Lazy stored propertyとImplicitly Unwrapped Optionalを組み合わせた時のおもしろい挙動
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
final class Person { | |
var name: String | |
lazy var greeting: ImplicitlyUnwrappedOptional<String> = { | |
return "Hello, \(self.name)" | |
}() | |
init(_ name: String) { | |
self.name = name | |
} | |
} | |
var p = Person("Steve") | |
p.name // => steve | |
p.greeting // => Hello, steve | |
p.name = "Tim" | |
p.greeting = nil | |
p.greeting // => Hello, Tim |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment