oscarg798
05/30/2019, 8:54 PMRuckus
05/30/2019, 8:55 PMoscarg798
05/30/2019, 8:56 PMRuckus
05/30/2019, 8:56 PMoscarg798
05/30/2019, 8:57 PMoscarg798
05/30/2019, 8:57 PMvar x: Map<String,()->Unit
Ruckus
05/30/2019, 8:57 PMFunction
is the super type of all functions in Kotlin, but it can't be invoked in any way.)oscarg798
05/30/2019, 8:57 PMx[key]?.invoke()
, but my functions can have one or two params, I’m trying to avoid having a whenCasey Brooks
05/30/2019, 8:58 PMRuckus
05/30/2019, 8:58 PMoscarg798
05/30/2019, 8:59 PMCasey Brooks
05/30/2019, 9:00 PMval x = mutableMapOf<String, ()->Unit>()
fun addCallback(key: String, cb: ()->Unit) {
x[key] = cb
}
fun elsewhere() {
addCallback("some_key") {
callOtherMethodWithLotsOfParams(param1, param2, ...)
}
}
oscarg798
05/30/2019, 9:02 PMoscarg798
05/30/2019, 9:12 PMclass X {
val map = HashMap<String, ()-> Unit>()
fun addKey(key:String, lambda: ()-> Unit){
map[key] = lambda
}
}
class Y(val param1: String, val param2: Int){
fun myLongFuction(){
print("doing some big work with $param1 and $param2")
}
}
class Z(){
fun myLongFuction(){
print("I do not have parameters")
}
}
class CLL {
@Test
fun cll(){
val x = X()
x.addKey("1"){
Y("Oscar", 1).myLongFuction()
}
x.addKey("2"){
Z().myLongFuction()
}
x.map["1"]?.invoke()
x.map["2"]?.invoke()
}
}