logo
down
shadow

How to pass functions to function in Kotlin


How to pass functions to function in Kotlin

By : LetThisNightExplode
Date : November 21 2020, 09:01 AM
Hope this helps In Kotlin 1.1 and later you can use bound callable references. e.g. bar(::foo).
In the second 1.0 Beta Candidate and other prior versions you could not pass around member functions. This issue was tracked as KT-6947.
code :


Share : facebook icon twitter icon
Kotlin cannot access kotlin.jvm.functions.Function1 when calling kotlin function with java lambda

Kotlin cannot access kotlin.jvm.functions.Function1 when calling kotlin function with java lambda


By : Lost_in_bits
Date : March 29 2020, 07:55 AM
it fixes the issue My problem got fixed when I configured Kotlin compiler and runtime for my Java module with the latest stable version (currently 1.3.30)
Just go to Tools > Kotlin > Configure Kotlin in Project > Android with Gradle and choose your Java module with Single module radio button selected then select your version and OK.
Kotlin Higher Order Function pass a function with variable number of arguments as the parameter

Kotlin Higher Order Function pass a function with variable number of arguments as the parameter


By : ahmad
Date : March 29 2020, 07:55 AM
wish helps you Whatever you are asking is against the Android best practices. May be mistakenly you have mentioned it in question. You want a function to execute Higher order function in a background thread and (pay attention here) return the value immediately (means block the calling thread until background task is completed).
As you must be knowing that network call should be async call and for that you have to pass an interface to the async method as parameter so that you can be notified when your Higher order function completed it's execution.
code :
async {
    var result = bean.createNewUser(User("","","Gosia","gosiak@gmail.com","pass"))
    // do whatever you want to do with result here        
}
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.21.2'
doAsync {
        var result = bean.createNewUser(User("","","Gosia","gosiak@gmail.com","pass"))
        // use result here to to further background task

        uiThread {
            // use result here if you want to update ui
        }
    }
compile "org.jetbrains.anko:anko-commons:0.10.0"
Is it possible to pass inline functions in Kotlin?

Is it possible to pass inline functions in Kotlin?


By : Serial C
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I'm facing the same problem. I have a similar function for casting Javascript's this object as you do. However, I've extended it with a lambda function with a receiver (the same thing that is used for type-safe builders in Kotlin except that my function returns Unit and is inlined).
code :
inline fun <T> withJsThis(body: T.() -> Unit) {
  js("this").unsafeCast<T>().body()
}
class LoginComponent {

  class Data {
    var username: String = "test@test.com"
    var password: String = ""
  }

  val data = {
    Data()
  }

  val template = """<form>
      <input type="text" v-model="username" placeholder="Email" /><br />
      <input type="password" v-model="password" placeholder="Password" /><br />
      <input type="submit" v-on:click.prevent="showInfo" />
    </form>"""

  val methods = object {

    val showInfo: () -> Unit = {
      withJsThis<Data> {
          console.log("METHOD: USER = $username, PASS = $password")
      }
    }

  }

  fun mounted() {
    withJsThis<Data> {
      console.log("MOUNTED: USER = $username, PASS = $password")
    }
  }

}
Best way to pass multiple lambda functions in Kotlin to handle success/failure

Best way to pass multiple lambda functions in Kotlin to handle success/failure


By : Sam
Date : March 29 2020, 07:55 AM
wish help you to fix your issue You can attach a name to each variable in kotlin. Change your code like this
code :
DeviceService.getDeviceStatus(
                onSuccess = { w ->
                    print("device")
                },
                onFailure = { e -> print(e) })
Kotlin - Pass array of functions as argument to function

Kotlin - Pass array of functions as argument to function


By : user2335014
Date : March 29 2020, 07:55 AM
wish helps you I got this to work by making two changes.
First, your updateMovies function as written takes an Array, when your listeners wants functions that take List. So, we can make this change:
code :
fun updateMovies(ids: List<String>) {
    TODO()
}
GetImdbIds(kodein).execute(1L, arrayOf(::updateMovies))
shadow
Privacy Policy - Terms - Contact Us © animezone.co