Check scope of the function call #2926
-
Hello team, is it possible to check if a function call is inside a specific scope? For example, let's say I want to invoke API calls only inside a helper function. One way to force this would be by context receivers, but these are deprecated, context parameters are not available yet, and this will require having context definition on every API method. With context receivers it would look like: interface ApiCallScope
object ApiCallScopeInstance : ApiCallScope
fun apiCall(block: ApiCallScope.() -> Unit) {
ApiCallScopeInstance.block()
}
interface Api {
context(ApiCallScope)
fun call()
}
val apiInstance: Api = object : Api {
context(ApiCallScope)
override fun call() {
}
}
fun work() {
apiCall {
apiInstance.call()
}
} Would it be possible to check by ktlint if the call is in the Another way would be make all API methods extension functions on interface ApiCallScope
object ApiCallScopeInstance : ApiCallScope
fun apiCall(block: ApiCallScope.() -> Unit) {
ApiCallScopeInstance.block()
}
interface Api {
fun ApiCallScope.call()
}
val apiInstance: Api = object : Api {
override fun ApiCallScope.call() {}
}
fun work() {
apiCall {
apiInstance.run {
call()
}
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
It doesn't look like something that ktlint can (nor should) support out of the box as the |
Beta Was this translation helpful? Give feedback.
Most likely that is not possible. Especially if the type is declared in another file.