Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
A question about Kotlin Function Type cast

Code fragment in Kotlin

  public actual fun <R, T> (suspend R.() -> T).createCoroutineUnintercepted(
    receiver: R,
    completion: Continuation<T>
): Continuation<Unit> {
    val probeCompletion = probeCoroutineCreated(completion)
    return if (this is BaseContinuationImpl)
        create(receiver, probeCompletion)
    else {
        createCoroutineFromSuspendFunction(probeCompletion) {
            (this as Function2<R, Continuation<T>, Any?>).invoke(receiver, it)
        }
    }
}

What I want to know is How (suspend R.() -> T) AKA Function Type can be recognized as BaseContinuationImpl which is a Classify Type even to cast as `Function2'.

Any help will be appreciated.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

There are no separate function types and class types. Function types are just types that can be executed with specific arguments and specific return type. They are interchangeable with Function0, Function1, etc. interfaces and they contain a single invoke() function.

We can implement a function type by our class:

class MyClass : (suspend () -> Unit) {
    override suspend fun invoke() {}
}

Now, let's get this code:

val lambda: (suspend () -> Unit) = {}

After disassembling we see that our lambda is compiled to:

final class FooKt$foo$lambda$1 extends kotlin/coroutines/jvm/internal/SuspendLambda implements kotlin/jvm/functions/Function1 {
    ...
    public final invoke(Ljava/lang/Object;)Ljava/lang/Object;
    ...
}

It extends SuspendLambda which is a subtype of BaseContinuationImpl. It also implements Function1 and contains invoke function which makes it a function type.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!