1

Here is my Kotlin code and when I build I receive Interface ImageSizeProvider does not have constructors

interface ImageSizeProvider {
    val spec: ImageSizeSpec
}

@FragmentScope
class ImageSizeProviderImpl @Inject constructor(
    private val mResources: Resources
) : ImageSizeProvider {
     override val spec: ImageSizeSpec
        get() = ...
}
and here is my java dagger interface ( I don't want to migrate this to Kotlin yet)




    @Module
public interface XXXModule {
    @Binds
    @FragmentScope
    ImageSizeProvider bindImageSizeProvider(ImageSizeProviderImpl imageSizeProvider);

I.S
  • 1,904
  • 2
  • 25
  • 48
  • I'm not sure what you're asking. Interfaces never have constructors because they are not classes. – Tenfour04 Oct 30 '20 at 19:56
  • I know but I receive that build error – I.S Oct 30 '20 at 20:49
  • Can you paste the exact error message you get with some context? (Line where it happens etc). The code you posted here seems fine so the issue is likely somewhere else. – al3c Nov 01 '20 at 19:01
  • I receive Interface ImageSizeProvider does not have constructors and it shows the line number that doesn't exist in dagger module and it shows in dagger module that includes another module which actually does the provides. – I.S Nov 02 '20 at 09:04
  • @I.S Can you paste that error message with some context? – al3c Nov 02 '20 at 10:01
  • That error should be preceded by a file name and position. Please check your code at the indicated position. – Nitrodon Nov 05 '20 at 15:28

1 Answers1

0

According to the language documentation, a property declared in an Interface is abstract. Your declaration:

val spec ImageSizeSpec

is therefore abstract and you must provide an overridden declaration in any class that implements the Interface. Your ImageSizeProviderImpl doesn't contain an overridden declaration of spec.

If you create an instance of ImageSizeProvider, this will fail because the variable spec is not initialized.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • I have ` override val spec: ImageSizeSpec` in the implementation. I just didn't copy that code – I.S Nov 04 '20 at 08:35
  • You must set a value in your override. You aren't allowed to have modifiable data in an `Interface`, only constants. – David Wasser Nov 04 '20 at 13:09
  • original code in java for interface looks like ImageSizeSpec getSpec(); so it's only should be getter – I.S Nov 04 '20 at 20:18
  • Understand. Am trying to follow all of this. Do you try to instantiate `ImageSizeSpec` anywhere? It appears that this may be the source of the problem. See https://stackoverflow.com/questions/43737785/kotlin-interface-does-not-have-constructors – David Wasser Nov 04 '20 at 22:41
  • Sorry, I meant "do you create an instance of `ImageSizeProvider`"? – David Wasser Nov 05 '20 at 20:05
  • Yes I have and I haven't noticed that I am initialising it somewhere. I thought I only inject it .thank you – I.S Nov 07 '20 at 12:59
  • I updated the answer. Do you want me to put anything else in the answer? I hope you were able to solve your problem. – David Wasser Nov 07 '20 at 14:06
  • 1
    No. problem, It is fixed, I haven't seen that it was initialised ,I thought I am only providing, you helped me to double check and see where I have it :) – I.S Nov 07 '20 at 14:31