can i create a function that accepts a callback no...
# getting-started
l
can i create a function that accepts a callback not a lambda?
c
How do you define „callback“? 😅
c
Sure, you can define your own interface and do things the older "java" style.
Copy code
interface MyCallback {
    fun doAThing(s: String)
}

fun doSomethingAsync(onComplete: MyCallback) {
    onComplete.doAThing("done")
}

class MainClass : MyCallback {
    fun runMain() {
        doSomethingAsync(this)
    }

    override fun doAThing(s: String) {
        println(s)
    }
}
And if you define the interface as a fun interface, then you can give the caller the choice to use either the Interface or lambda syntax:
Copy code
fun interface MyCallback {
    operator fun invoke(s: String)
}

fun doSomethingAsync(onComplete: MyCallback) {
    onComplete("done")
}

fun runMain() {
    doSomethingAsync {
        println(it)
    }
}
Fundamentally, there's no difference between a lambda and an interface, though. Kotlin compiles lambdas down to interfaces under the hood (
Function1<T1, R>, Function2<T1, T2, R>
, etc), . It's just a syntax thing
l
first of all, IDK how do to reply to a particular message in slack. if that's not possible and how ppl do that in slack is by copy-paste slice/part of Original message as a code/code-block styled. then I'd say "don't be hard on yourself and move to discord" guys please 🙏😔 now, to @Chrimaeon's "how do I define a callback": sorry I forgot this is jvm, everything has to be an object. I can't pass independent functions like JS or any highly functional-sided programming language. kotlin performs an extra step to copy paste in case of inline functions but yeah roughly the same thing I guess. I observe I have a problem with lambda, that I feel physical pain when I see lambda being used in kotlin. signature, definition, usage of a function are all at different places, I have to move my eyes and be very very attentive to understand the flow. but I can't do anything, I have to accomplish my dream of becoming an android app dev and for me kotlin is a hurdle not a companion. I have to pass this hurdle to reach the end. and I will.
c
"don't be hard on yourself and move to discord" guys please 🙏😔
Discord added Slack-like threads a few years ago 😅
I can't pass independent functions like JS or any highly functional-sided programming language.
You can;
Copy code
fun foo(e: () -> Unit) = …

foo { println("hello") }
This is passing a function directly. It's the same as in JS. You can also pass function references:
Copy code
fun bar() {
    println("hello")
}

foo(::bar)
Again, that's the same as in JS
🙏 1
l
but slack didn't add a single good feature everyone needs ie right click and select "reply" to a particular message.
discord adopted a good feature from slack though. W discord.