anvil icon indicating copy to clipboard operation
anvil copied to clipboard

No way to create a `FunctionReference` to a top level function 😞

Open efemoney opened this issue 3 years ago • 2 comments

We are looking at code gen use case such as below

@ContributesJsonAdapter
internal fun Moshi.Builder.myAdapter(
  one: One,
  @Named("foo") another: Another,
) {
  add(MyAdapter(one, other))
}

that will generate:

@ContributesMultibinding(AppScope)
class MyAdapterFn_Registrar(
  one: One,
  @Named("foo") another: Another,
): JsonAdapterRegistrar {

  override fun register(moshi: Moshi.Builder) {
    moshi.myAdapter(one, another)
  }
}

There are no out-of-the-box APIs to get all top level functions (at least not as cleanly as with ClassRefeferences). Now even when we get the top-level KtNamedFunctions from the file, we cannot convert them to FunctionReferences because that API needs a declaringClass ClassReference which (at least to my knowlege) we cannot get from KtNamedFunction or KtFile

Also tried KtFile.facadeClass().[...] and that was a dead end too.

How can we represent a top level function as a FunctionReference?

efemoney avatar Oct 07 '22 12:10 efemoney

Yes, I agree that this is an issue. I'd not advocate for an API to get all top level functions, but not being able to wrap a top level function with a FunctionReference is a problem. We didn't have a use case internally for this until now, but it's a valid one.

vRallev avatar Oct 07 '22 16:10 vRallev

We also have a similar use case where we want to generate something based on annotated composables which are usually top level functions

gabrielittner avatar Oct 07 '22 17:10 gabrielittner