2

I am looking for the equivalent of the command @escaping for kotlin if it exists?

func fetch(response: @escaping (PriceResponse?) -> Void)
  • 5
    What part of @escaping are you trying to capture here? Kotlin's memory management is completely different than Swift's. I would expect every Kotlin closure to the equivalent of escaping (but also that it shouldn't matter). What you're describing in your comments to Pranavan don't sound like escaping. They sound more like a coroutine (which doesn't exist in Swift today). – Rob Napier Aug 14 '18 at 13:25
  • @escaping is primarily a thing for Swift internals. Non-escaping closures can be passed as a simple function pointer (and possibly be inlined) – whereas escaping closures needs to be wrapped in an object that captures the state of the scope in which it is declared. – Trenskow Apr 20 '22 at 12:21

3 Answers3

4

Yes. exists. Also The syntax is similar to swift:

fun fetch(callback: (PriceResponse) -> Unit){ }

change PriceResponse to whatever you're returning

Also edited

Then change your function invocation from fetch() to

fetch {
    callingYourAPI(here) // or you can get named argument
    // do some more stuff
}
Pranavan SP
  • 1,785
  • 1
  • 17
  • 33
  • 1
    I'm talking about the command @escaping, because in kotlin if I call the response and I want to leave the function I would have to write then return. Because in my function I have a condition that calls response several times, so every time I have to write return –  Aug 14 '18 at 12:13
  • @mickael don't you want to implement completion handler in kotlin? – Pranavan SP Aug 14 '18 at 12:19
0

If your question is about removing the restriction of calling @escaping function inside the fetch() function - then you don't need to do anything special in Kotlin - just define the function as incoming argument.

Some more theory:

  • all closures are considered "escaping" by default.
  • however if you would like to define them "non escaping" - you can do it with the help of the callsInPlace contract.

See for more info: https://stackoverflow.com/a/65989221/3134602

Alex
  • 521
  • 7
  • 7
-1

I think that according to this https://kotlinlang.org/docs/reference/lambdas.html all closures in kotlin are nonescaping and there is no way to make escaping.

UPD: Similar functionality can be achieved with help of kotlin corutines. More you can find here: https://kotlinlang.org/docs/reference/coroutines.html

redlabrat
  • 507
  • 7
  • 14
  • 1
    As far as I can see the Kotlin reference manual doesn't explicitly mention the issue but the implication is all lambda's are what Apple call "escaping" in Swift. This is not surprising at all as that is how lambda's are normally handled in programming languages; the non-escaping/escaping distinction is Apple surfacing a compiler optimisation which they either: believe should be manual; or cannot make automatic. However as @RobNapier has commented it is unclear that the OP is actually taking about `@escaping` at all... – CRD Aug 15 '18 at 09:52
  • 1
    It looks, that I misunderstood @escaping notation in swift and you right, that all lamdas are usually implemented as escaping as it in kotlin. And my answer is not correct. – redlabrat Aug 15 '18 at 10:27