Restricting Function Execution to other Suspend Functions

As of Kotlin version 1.3 there is now a RestrictsSuspension annotation that allows for are that restricts Classes or Interfaces to only be invoked from suspend extensions/functions. However, you cannot apply this annotation to functions.

The ability to restrict extension functions to be only called from other suspend functions would allow the compiler to easily distinguish between multiple overrides.


sealed class Result<out A, out E : Exception> {
     data class Success<out A> (val value: A) : Result<A, Nothing>()
     data class Failure<out E : Exception> (val error: E) : Result<Nothing, E>()

     companion object {
         fun <V : Any, E : Exception> of(f: () -> V): Result<V, E> = try {
         } catch (ex: Exception) {
             Failure(ex as E)

suspend fun <V: Any, E : Exception>Result.of(f: suspend () -> V) : Result<V, E> = try {
} catch (ex: Exception) {
     Result.Failure(ex as E)

The ability to mark a function as only callable from other suspend or coroutine builders would simplify a number of operations like the one above.