Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No way to create a FunctionReference to a top level function #644

Closed
efemoney opened this issue Oct 7, 2022 · 2 comments · Fixed by #675
Closed

No way to create a FunctionReference to a top level function #644

efemoney opened this issue Oct 7, 2022 · 2 comments · Fixed by #675
Labels
custom code generator Related to the compiler-api module enhancement New feature or request

Comments

@efemoney
Copy link

efemoney commented Oct 7, 2022

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?

@vRallev vRallev added enhancement New feature or request custom code generator Related to the compiler-api module labels Oct 7, 2022
@vRallev
Copy link
Collaborator

vRallev commented Oct 7, 2022

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.

@gabrielittner
Copy link
Contributor

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

@efemoney efemoney changed the title No way to create a FunctionReference to a top level function 😞 No way to create a FunctionReference to a top level function Oct 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
custom code generator Related to the compiler-api module enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants