Giter VIP home page Giter VIP logo

Comments (5)

sergejsha avatar sergejsha commented on June 22, 2024 1

What is the rule in Magnet anyways here? Imagine I have provided A and B, which one would be the constructor Magnet would pick?

IIRC @Instance is only allowed on static methods and types, not constructors, so I have no way to pick a specific constructor Magnet should use for construction, right?

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 computationScheduler and ioScheduler parameters. Current Magnet version overwrites those values with the ones from the scope, if they are available there. This ticket is about to respect given Kotlin defaults and keep them intact.

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 TestScheduler or Schedulers.trampoline() to the instance and to test what ever needed, including debouncing and timeouts.

from magnet.

realdadfish avatar realdadfish commented on June 22, 2024

What is the rule in Magnet anyways here? Imagine I have provided A and B, which one would be the constructor Magnet would pick?

@Instance(type = Foo::class)
class Foo(private val a: A, private val b: B) {
     constructor(a: A) : this(a, B())
}

IIRC @Instance is only allowed on static methods and types, not constructors, so I have no way to pick a specific constructor Magnet should use for construction, right?

from magnet.

realdadfish avatar realdadfish commented on June 22, 2024

Ok, I see. I grouped together the various schedulers and more into a SchedulerProvider, of which I have a test implementation, so it is not much of a hassle for me to provide one instance of that provider in the root scope to have it available everywhere. I understand however if you inject individual instances of Scheduler you'd have to manually @Classifier them for injection, so it is easier to just use the static instance methods to return default values.

from magnet.

sergejsha avatar sergejsha commented on June 22, 2024

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.

from magnet.

sergejsha avatar sergejsha commented on June 22, 2024

Kotlin supports generation of overloaded Java constructors for a kotlin constructor with default arguments when @JvmOverloads constructor syntax is used.

@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.

from magnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.