Reflections and extension function confusion


#1

Given:

class Foo {}
fun Foo.bar() {}
fun main(args: Array<String>) {
  check(Foo::class.memberExtensionFunctions.size > 0)
}

Why does the check fail? I would expect Foo.bar() to be in the memberExtensionFunctions list. I also tried declaredMemberExtensionFunction (though I’m not sure what the difference is) with the same result. Am I missing something?


#2

Why does the check fail? I would expect Foo.bar() to be in the memberExtensionFunctions list.
I also tried declaredMemberExtensionFunction (though I’m not sure what the difference is) with the same result. Am I missing something?

The list contains member extension functions, i.e. those that are both members and extensions at the same time:

class C {
    fun D.ext() {} // this is a member extension function
}

#3

I see, thanks. Is there any way to find all the extension functions for Foo, or with your example, D?


#4

The task of finding all extension functions for Foo is equivalent to finding all methods which have Foo as the first parameter. Neither of these is possible without accessing every single class in your application through reflection.