Created
March 4, 2016 03:54
-
-
Save omochi/4d703e842903c41ac1cf 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
$ swift | |
Welcome to Apple Swift version 2.1.1 (swiftlang-700.1.101.15 clang-700.1.81). Type :help for assistance. | |
1> | |
2. | |
3. class Cat { | |
4. lazy var voice: String = self.nya() | |
5. func nya() -> String { | |
6. return "nya" | |
7. } | |
8. deinit { | |
9. print("deinit") | |
10. } | |
11. } | |
12. | |
13. class CatImpl { | |
14. var voiceInited: Bool = false | |
15. var voiceValue: String = "" | |
16. var voice: String { | |
17. get { | |
18. if (!voiceInited) { | |
19. voiceValue = computeLazyValue { | |
20. self.nya() | |
21. } | |
22. } | |
23. return voiceValue | |
24. } | |
25. } | |
26. private func computeLazyValue(@noescape proc: () -> String) -> String { | |
27. return proc() | |
28. } | |
29. func nya() -> String { | |
30. return "nya" | |
31. } | |
32. deinit { | |
33. print("deinit") | |
34. } | |
35. } | |
36. | |
37. func testScope() { | |
38. let _ = Cat() | |
39. let _ = CatImpl() | |
40. } | |
41. | |
42. print("testScope enter") | |
43. testScope() | |
44. print("testScope exit") | |
45. | |
testScope enter | |
deinit | |
deinit | |
testScope exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment