-
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
Support default values in constructors and methods in Kotlin #77
Comments
What is the rule in Magnet anyways here? Imagine I have provided @Instance(type = Foo::class)
class Foo(private val a: A, private val b: B) {
constructor(a: A) : this(a, B())
} IIRC |
This is correct. Magnet will fail to compile because it expects a single non-private constructor. This was a deliberate design decision. The reason for it is simpler annotation syntax (also for Kotlin) and enforced single-constructor design, which is, in my opinion, cleaner than multi-constructor one, and Kotlin constructors kind of confirming that. This ticket is rather about default parameters in constructor. Let's take the example down below. @Instance(type = GalleryScannerService::class)
internal class DefaultGalleryScannerService(
mediaStoragePermissions: MediaStoragePermissions,
mediaStorageObserver: MediaStorageObserver,
mediaStorageSyncer: MediaStorageSyncer,
computationScheduler: Scheduler = Schedulers.computation(),
ioScheduler: Scheduler = Schedulers.io()
) : GalleryScannerService It declares default values for You might have a question why those two are in constructor at all. They are there for easier testing. There is no need to apply any special rules in tests. It's enough to provide |
Ok, I see. I grouped together the various schedulers and more into a |
After more detailed analysis of the problem I realized that the constructor which sets default values in Kotlin is implemented as a synthetic method with Kotlin-specific parameters. Thus calling it properly from Java is practically impossible. The only option to respect default values in constructor would be to generate Kotlin code instead of Java code, which goes beyond the scope of this ticket. |
Kotlin supports generation of overloaded Java constructors for a kotlin constructor with default arguments when @Instance(type = TemplateObserver::class)
internal class DefaultTemplateObserver @JvmOverloads constructor(
private val database: Database,
private val marshaller: Marshaller,
private val templateDao: TemplateDao,
ioScheduler: Scheduler = Schedulers.io()
) : TemplateObserver { ... } Because those constructors can be called from Java, Magnet will also be able to call them from generated factories. |
Magnet ignores default values while injecting parameters into constructors and methods in Kotlin.
Expected behavior: Magnet does not inject values for parameters with defaults.
The text was updated successfully, but these errors were encountered: