Created
August 18, 2021 14:39
-
-
Save nakamura-to/709e70370989101a25762d7b7ef5cbde 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
fun String.globalExtension() { | |
println(this) | |
} | |
class Scope { | |
fun String.narrowExtension() { | |
println(this) | |
} | |
} | |
fun dsl(block: Scope.()-> Unit) { | |
block(Scope()) | |
} | |
fun main() { | |
"どこからでも呼び出せる拡張関数".globalExtension() | |
dsl { | |
"どこからでも呼び出せる拡張関数".globalExtension() | |
"特定のラムダ式の中でしか呼び出せない拡張関数".narrowExtension() | |
} | |
// これはコンパイルエラー | |
// "特定のラムダ式の中でしか呼び出せない拡張関数".narrowExtension() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment