-
Notifications
You must be signed in to change notification settings - Fork 18
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
@Instance(types)
must accept a classifier per type
#76
Comments
@Instance(types)
should accept a classifier per type@Instance(types)
must accept a classifier per type
I'm not sold to the concept of having two types provided by one class like this. Why not do it like this (if @Instance(type = FooBar::class)
internal class FooBar(): Foo, Bar {}
@Instance(type = Foo::class, classifier = "foo")
fun provideFoo(foobar: FooBar): Foo = foobar
@Instance(type = Bar::class, classifier = "bar")
fun provideBar(foobar: FooBar): Bar = foobar Yes, it is slightly more declarative work, but also more clean IMHO. |
This case is very specific, but sometimes there is no better way around. The typical one I came across was when I wanted to implement an extension interface (e.g. @Instance(types = [PlayerFeature::class, PlayerListener::class])
internal class UndoPlayerFeature(): PlayerFeature, PlayerListener {
override fun onCustomAction(action: String, extras: Bundle?) // from PlayerFeature
override fun onPlayerEvent(event: PlayerEvent) // from PlayerListener
} Recently I needed it when I was implementing ViewModel delegates, where ViewModel itself was implemented using predictable state container pattern. There were two interfaces like
It's surely possible, but I would not do it like that because
Proposed change does not add a new feature but suppose rather remove an artificial restriction of the current syntax. |
Understood your points, I guess it is just a very specific use case that is modeled in a rather general purpose tool, but no hard feelings here. Just from an API point of view I'd say that the additional (mental) complexity that an aliasing mechanism brings onto the table would have to be reasoned by more than one particular, very specific use case. Like, you couldn't (or maybe you could, but would you?) prevent misuse of the feature like this, right? @Instance(type = Foo::class, classifier = "foo")
@InstanceAlias(type = Foo::class, classifier = "bar")
internal class Foo() {} But again, no hard feelings :) |
The more I use Magnet and apply interface segregation principle, the often I need this feature. I might be biased, but I see it as an advantage having this kind of syntax under my fingers then going chatty dagger way.
My gut feeling tells this is not a misuse, but I cannot say anything certain about it. It needs to be tried out. If this syntax/concept causes issues, it can always be deprecated and replaced by a better one. |
Postponed until there is a real need for it. |
When declaring
all types use shared classifier value. Expected behavior: classifier should be declarable per type.
A solution could be to remove
types
from theInstance
annotation and apply a newInstanceAlias
annotation for each new instance type as following:The text was updated successfully, but these errors were encountered: