Kotlin:Double Colon与Lambda在类似的类别中

mle*_*fer 4 kotlin

我理解我的第一个第二个例子使用传入一个参数的lambda函数.我试图理解为什么它与我的第二个样板示例不同,后者使用双冒号而不是lambda.(仍然是一个kotlin newb试图将我的脑袋缠绕在来自蟒蛇背景的双冒号)

class Service(services: PluginServiceHub) {
    init {
        services.registerFlowInitiator(Landlord::class.java) { Landlord(it) }
    }
}
Run Code Online (Sandbox Code Playgroud)

VS

class Service(services: PluginServiceHub) {
    init {
        services.registerFlowInitiator(IssuanceRequester::class.java, ::Issuer)
    }
}
Run Code Online (Sandbox Code Playgroud)

这究竟::Issuer代表什么?

hot*_*key 6

假设有一个类Issuer,::Issuer将是对其构造函数的函数引用.采用适当数量的参数(在本例中为一个)的构造函数将被解析和使用,这相当于一个lambda { Issuer(it) }.

如果没有这样的类,Issuer则将使用名为并接受一个参数的函数(如果存在).

请参阅:Kotlin中是否有构造函数引用?