I understand my first second example uses a lambda function with a single parameter passed in. I'm trying to understand why it would be different from my second boilerplate example where a double colon is used instead of a lambda. (still a kotlin newb trying to wrap my head around double colons coming from a python background)
class Service(services: PluginServiceHub) {
init {
services.registerFlowInitiator(Landlord::class.java) { Landlord(it) }
}
}
VS
class Service(services: PluginServiceHub) {
init {
services.registerFlowInitiator(IssuanceRequester::class.java, ::Issuer)
}
}
What does the ::Issuer
represent exactly?
Assuming there is a class Issuer
, ::Issuer
will be a function reference to its constructor. A constructor taking the appropriate number of arguments (one in this case) will be resolved and used, which is equivalent to a lambda { Issuer(it) }
.
If there's no such a class, a function named Issuer
and taking one argument will be used, if it exists.
See: Are there constructor references in Kotlin?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With