Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.8k views
in Technique[技术] by (71.8m points)

android - Inject Interface with Generics in Hilt

I am trying to use Hilt to inject a generic interface implementation but it keep gives me errors

My interface looks like this:

interface Validator<in T> {
  fun validate(value: T): Boolean
}

And a typical implementation of it can be like this:

class FullNameValidator @Inject constructor() : Validator<String>{

   override fun validate(value: String): Boolean {
      val isValid = //Validation logic

      return isValid
    }
}

Now the way I am trying to use Hilt, i've tried to use @Bind method in a @Module like this

@Binds
abstract fun bindFullNameValidator(validator: FullNameValidator) : Validator<String>

Like i am doing with all interfaces implementations and its working just fine But here, Hilt claims that it cannot find a way to inject the String object which is the generic type.

Is there a proper way to inject such interface


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...