How does one check to see if a property value that...
# getting-started
s
How does one check to see if a property value that is declared as type Any has been initialized with a function? i.e.
Copy code
class SomeClass {
    var someProp: Any? = null
}

fun main() {
   val someClass = SomeClass()
    someClass.someProp = {
        println("I do things")
    }

    // else where

    if(someClass.someProp is Function<*>){
        someClass.someProp() // invoke not found...
    }
}
a
Seems you cast it to function without parameters and result type, so there is really nothing to invoke
s
That maybe, what I'm looking for is a way to check at runtime that the property is callable. The
is Function<*>
where it isn't doing what I would expect doesn't mean it isn't doing what it was intended to do and I just need to learn it. I meant it only as, "how do I accomplish this"?
For instance
is Lambda
would be nice.
m
First of all: Don't use Any if you aren't forced to do so. But anyways, there is a solution to your problem: https://pl.kotl.in/aFGOJVOh3 The problem is that you just chekc for Function and not for Function0 (number of args). The second problem is that theoretically an other coroutine / Thread could reassign the var. That's the reason why your smart cast isn't working. If you assign it to a val it isn't mutable, which means smart cast is working now.
s
@molikuner Thank you! I had not yet learned about
FunctionN
. Perfect.