Last active
July 18, 2017 05:39
-
-
Save ezura/12cd5fdffdcf93ece54f27c72a090d42 to your computer and use it in GitHub Desktop.
kotlin 可視性実験
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
class A { | |
private fun f() = print("private") | |
fun _f() = f() // use `private fun f()` | |
} | |
fun A.f() = print("extension") | |
fun A._f() = print("extension _f") // ignored 😖 | |
/* | |
val a = A() | |
a.f() // extension | |
a._f() // private | |
*/ |
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
class A { | |
// private fun f() = print("private") | |
fun _f() = f() // use extension `fun f()` | |
} | |
fun A.f() = print("extension") | |
/* | |
val a = A() | |
a.f() // extension | |
a._f() // extension | |
*/ |
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
class C { | |
private fun f() = print("private") | |
} | |
// error! recursive call | |
// fun C.f() = f() | |
// error! cannnot access `f`. it is private in `C` | |
// fun C._f() = f() |
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
open class D { | |
fun f() = print("Super class: private") | |
} | |
fun D.f() = print("Super class: extension f") | |
fun D._f() = print("Super class: extension _f") | |
class _D: D() { | |
// private fun f() = print("Sub class: private") | |
// !error: overrides nothing | |
// override fun _f() = f() // use extension `fun f()` | |
} | |
fun _D.f() = print("Sub class: extension") | |
fun _D._f() = print("Sub class: extension _f") | |
/* | |
import accessControl.* | |
_D().f() // Super class: private | |
_D()._f() // Sub class: extension _f | |
*/ |
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
class E { | |
fun f() = print("class") | |
} | |
fun E.f(): String = "extension _f" | |
/* | |
E().f() | |
class | |
val s: String = E().f() | |
error: type mismatch: inferred type is Unit but String was expected | |
val s: String = E().f() | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Warning:(13, 7) Kotlin: Extension is shadowed by a member: public final fun _f(): Unit
って言われてるな…