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

Generics #80

Closed
realdadfish opened this issue Apr 4, 2019 · 2 comments
Closed

Generics #80

realdadfish opened this issue Apr 4, 2019 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@realdadfish
Copy link
Contributor

So I guess this came up before, but anyways :) I want to be able to inject generic implementations and right now Magnet hinders me to do that because of some class name validation issue:

const val RATINGS_STORAGE = "ratings-storage"

data class RatingsModel(...)

interface ModelStore<T> {
    fun load(): T?
    fun save(data: T)
}

@Instance(type = ModelStore::class, classifier = RATINGS_STORAGE)
fun provideRatingsModelStore(): ModelStore<RatingsModel> {
    return /* create the store for the RatingsModel */ 
    }
}

@Instance(type = RatingsManager::class)
class RatingsManager internal constructor(
    @Classifier(RATINGS_STORAGE) private val store: ModelStore<RatingsData>
) {
   ...
}

This fails with

error: Method must return instance of path.to.ModelStore as declared by interface magnet.Instance
    public static final de.aoksystems.ma.abp.modelstore.ModelStore<RatingsData> provideRatingsModelStore()

Now I thought maybe I could outsmart the validation and use

fun provideRatingsModelStore(): ModelStore<*> { ... }

but this didn't work either. And naturally, Kotlin doesn't let me use the plain ModelStore type without any generic arguments, as I could do with Java:

public class StaticProvision {
    @Instance(type = ModelStore.class, classifier = RATINGS_STORAGE)
    static ModelStore provideRatingsModelStore() {
        return /* create the store for the RatingsModel */;
    }
}

This version of course compiles just fine.

What are my options here (beside starting to write Java code again :))?

@realdadfish
Copy link
Contributor Author

That was quick, thank you! 🙏

@sergejsha
Copy link
Owner

You are welcome. The fix is available in 3.1-beta1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants