Last active
December 6, 2018 14:23
-
-
Save thara/838f2ee3bcc166ff6bcea00860d38c49 to your computer and use it in GitHub Desktop.
Kotlin isInitialized
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
| class A { | |
| lateinit var s: String | |
| fun print() { | |
| println(s) | |
| } | |
| } | |
| fun main(args: Array<String>) { | |
| A().print() | |
| } |
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
| class B { | |
| lateinit var s: String | |
| fun print() { | |
| if (::s.isInitialized) { | |
| println(s) | |
| } | |
| } | |
| } | |
| fun main(args: Array<String>) { | |
| B().print() | |
| } |
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
| class C { | |
| lateinit var s: String | |
| fun print() { | |
| if (s.isInitialized) { // この行でUnresolved referenceというコンパイルエラーが発生する | |
| println(s) | |
| } | |
| } | |
| } | |
| fun main(args: Array<String>) { | |
| C().print() | |
| } |
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
| class D { | |
| var s: String // コンパイルエラー: Property must be initialized | |
| fun print() { | |
| if (::s.isInitialized) { // コンパイルエラー: This declaration can only be called on a reference to a lateinit property | |
| println(s) | |
| } | |
| } | |
| } | |
| fun main(args: Array<String>) { | |
| D().print() | |
| } |
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
| - if (::x.isInitialized) { | |
| + if (x.isInitialized) { |
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
| class X(val isInitialized: Boolean) {} | |
| class Z { | |
| lateinit var x: X | |
| fun print() { | |
| if (::x.isInitialized) { | |
| print("Initialized") | |
| } else { | |
| print("Not initialized") | |
| } | |
| } | |
| } | |
| fun main(args: Array<String>) { | |
| val z = Z() | |
| z.x = X(false) | |
| z.print() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment