# Restricting Function Execution to other Suspend Functions

**URL:** <https://discuss.kotlinlang.org/t/restricting-function-execution-to-other-suspend-functions/15898>\
**Category:** Language Design\
**Created:** [January 14, 2020, 8:06pm UTC](https://discuss.kotlinlang.org/t/restricting-function-execution-to-other-suspend-functions/15898 "2020-01-14T20:06:18Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![james-novino](https://avatars.discourse-cdn.com/v4/letter/j/e9a140/32.png) [@james-novino](https://discuss.kotlinlang.org/u/james-novino)\
**Post date:** [January 14, 2020, 8:06pm UTC](https://discuss.kotlinlang.org/t/restricting-function-execution-to-other-suspend-functions/15898/1 "2020-01-14T20:06:18Z")

</div>

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.

i.e.

```
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 {
             Success(f())
         } catch (ex: Exception) {
             Failure(ex as E)
        }
    }
}

@RestrictsSuspension
suspend fun <V: Any, E : Exception>Result.of(f: suspend () -> V) : Result<V, E> = try {
     Result.Success(f())
} 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.
