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
AssistedInject for a type with generic parameter produces code that doesn't compile #2279
Comments
@lwasyl, thanks for the repro case! I agree we should support this. I should be able to get a fix out soon. |
Btw, now that I've had some time to look into this, just wanted to clarify some of the restrictions I think we'll have to make with this feature. We would only allow this feature if the type parameter is not used as a binding (i.e not used in constructor/field/method injection) within the assisted inject class. As an example, take the assisted inject class shown below final class Foo<T1, T2, T3, T4, T5> {
@Inject T1 t1;
private final T2 t2;
private final T3 t3;
private final T3 assistedT3;
private final T4 assistedT4;
private T5 t5; // e.g. this can be set via a setter method
@AssistedInject
Foo(T2 t2, T3 t3, @Assisted T3 assistedT3, @Assisted T4 assistedT4) {
this.t2 = t2;
this.t3 = t3;
this.assistedT3 = assistedT3;
this.assistedT4 = assistedT4;
}
} In this case, // This is a valid factory definition
@AssistedFactory
interface FooFactory<T1, T2, T3, T4, T5> {
Foo<T1, T2, T3, T4, T5> create(T3 assistedT3, T4 assistedT4);
}
// This is a valid factory definition
@AssistedFactory
interface FooFactory<T1, T2, T3> {
<T4, T5> Foo<T1, T2, T3, T4, T5> create(T3 assistedT3, T4 assistedT4);
} However, the factory below is not a valid factory because even though // This is an **invalid** factory definition
@AssistedFactory
interface FooFactory<T1, T2> {
<T3, T4, T5> Foo<T1, T2, T3, T4, T5> create(T3 assistedT3, T4 assistedT4);
} |
@lwasyl, just a heads up that we're still considering this but it's unlikely it'll be in the next release. For now, I'm going to add some validation in our processor so that at least we'll catch this and fail fast with a better error message rather than generating invalid java. |
…er with the @AssistedFactory creator method. We're considering supporting this feature in the future, but until then we're adding a better error message. See #2279 RELNOTES=Issue #2279: Adds better error message when trying to use a type parameter with @AssistedFactory creator method. PiperOrigin-RevId: 354324578
…er with the @AssistedFactory creator method. We're considering supporting this feature in the future, but until then we're adding a better error message. See #2279 RELNOTES=Issue #2279: Adds better error message when trying to use a type parameter with @AssistedFactory creator method. PiperOrigin-RevId: 354389636
Hello, I tried using the above suggestions but I am still getting an error.
|
@gusuly0rum which suggestion are you referring to? Unfortunately, I haven't had time to get to this yet, so your code is failing because this feature doesn't exist yet. |
@bcorso I was referring to your example with the valid and invalid factory examples. |
All of the the type arguments must currently be specified at the time
|
@bcorso I also ran into the same error, it will be good if this issue is fixed ASAP. Because this will give enough freedom in working with Hilt. Now I have to my rewrite my project di from scratch using Dagger. |
Hi @D-Soft-Tech, Can you give more details about your use case? In particular, I'd like to better understand why not having this feature would cause you to have to rewrite your project's DI from scratch. |
Hi, I think I might have a somewhat motivating use case. In class MappingFinder, the goal is to make the generic parameter Note: |
MappingFactory might be a better example, because it is already using |
Any updates on this? |
I have a use case for something that produces this error message, I think it's in scope for this ticket. I have some object, I've got a separate shared UI state component called Ideally, I'd be able to do something like
( My only option currently is to have separate parameters for each of the interfaces I want to implement. That's not ideal, since there's a few more than two, and it's all just the same object being passed in. |
I also would like to use a generic type parameter with @AssistedInject, but in a different way. I would like to use:
This gives an error during generation of the code in version 2.50 (and maybe others), referencing to this issue. As a workaround, I tried with wildcards as well:
This will successfully generate the code, but will fail during compilation. Now I'm a bit stuck here. Is there any chance to get stuff like this working besides not using @AssistedInject? |
this future can be useful if get implemented. |
Dagger 2.31
The following class generates code that doesn't compile:
generated code:
which fails with
The text was updated successfully, but these errors were encountered: