How can I make a method callback whos parameters c...
# announcements
s
How can I make a method callback whos parameters can be redefined wiithout using a varargs <Any> parameter
r
whos parameters can be redefined
Can you elaborate on that? You can't redefine a function's parameters, unless I'm not understanding what you're asking.
Maybe share an example or some pseudo-code of what you are trying to achieve
s
like for example
Copy code
UI.kt
    inner class callbacks {
        fun play() {}
    }

    fun setup_buttons(Play : android.widgets.button) {
        Play!!.setOnClickListener { callbacks().play(Play) }
    }
    
    Player.kt
    fun play_callback(Play : android.widgets.button) {
        if (!UI.variables().mPlayerAdapter!!.isPlaying()) {
            UI.variables().mPlayerAdapter!!.play()
            Play!!.text = "Pause"
        } else {
            UI.variables().mPlayerAdapter!!.pause()
            Play!!.text = "Play"
        }
    }
//        UI.callbacks.play = play_callback
or something
as i know that you can define custom bodies with
Copy code
fun main(args : Array<String>){
    val functions = mutableMapOf<String, () -> Unit>()
    
    functions["first"] = { println("heyo!") }
    println(functions)
    functions["first"]?.invoke()
}
but im trying to do the same for a paramater
r
That's not a "custom body", it's a lambda. It still has to follow the definition (
() -> Unit
is basically just shorthand for "an interface with a method `invoke(): Unit`"). If you want to accept arbitrary arguments, that has to be part of the definition.
s
Can the definition be defined in the same manner as the lambda
r
I'm not sure what you mean
d
Generics are the only thing that come to mind, but I’m not sure what the original question is asking, ie.
fun <T> doSomething(param: T) { ... }
would accept anything.
But I probably need an actual use-case to provide a more useful suggestion.
s
Wouldn't that be the same as
fun doSomething(param: <Any>) { ... }
?
d
Yes, except depending on what you need to do,
T
can do some other stuff. Again it’s hard to provide a helpful suggestion when I don’t really understand what your use case is.