Comments (5)
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.
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.
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.
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.
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)
- Unsupported KotlinClassMetadata of type null after update to Kotlin 1.5.20 HOT 1
- Provide SavedStateRegistryOwner in a magnet.Factory interface HOT 5
- No stetho-enabled processes running - with Magnet 3.6-rc1 HOT 4
- java.lang.IllegalStateException: Single instance requested, while many instances are stored HOT 3
- Cannot verify type declaration HOT 1
- When release version 3.7 ? HOT 1
- Magnet for Kotlin 1.8. HOT 1
- Injection overrides HOT 15
- Lazy construction HOT 26
- `@Instance(types)` must accept a classifier per type HOT 5
- Generics HOT 2
- Processor fails to generate Factory when custom scope name is used
- Lazy injection in Kotlin HOT 1
- Determine placement of instances HOT 16
- Implement scope visitor HOT 5
- 3.3-rc3 packaging issue HOT 6
- Wrong placement of instances when "getMany" and "sibling types" are used HOT 1
- Type binding HOT 17
- SelectorAttributeParser is treating nots as delimiters HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from magnet.