I am processing a collection of instances of MyCustomType
as follows:
fun runAll(vararg commands: MyCustomType){
commands.forEach { it.myMethod() }
}
Besides instances of MyCustomType
, I'd like to pass and process lambdas of type () -> Unit
, something like this:
fun runAll(vararg commands: Any){
for(command in commands){
if (command is MyCustomType){
command.myMethod()
} else
if(command is () -> Unit){
command.invoke()
}
}
}
The line if(command is () -> Unit){
does not compile with the following message: Cannot check for instance of erased type: () -> Unit
.
Is there a way to check if an object is () -> Unit
at runtime?
I have seen another answer that recommends using wildcards. I do not think that is relevant: my code does not use generics.