Reflections and extension function confusion

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?

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
}
1 Like

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

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.

1 Like