如何解决这是哪个 kotlin 语言功能
我正在学习 kotlin DSL,特别是 Teamcity,我看到了一个我还不太了解的初始化模式
这是代码
package org.arhan.kotlin
fun main() {
val project = project {
configuration {
step {
name = "step 1"
command = "hi"
}
customstep {
name = "flang"
anotherCommand = "derp"
command = "1111"
}
}
}
println(project.configurations[0].steps[1].command)
}
fun project(block: Project.() -> Unit): Project {
return Project().apply(block)
}
fun Project.configuration(block: Configuration.() -> Unit): Configuration {
val configuration = Configuration().apply(block)
configurations.add(configuration)
return configuration
}
fun Configuration.step(block: Step.() -> Unit): Step {
val step = Step().apply(block)
steps.add(step)
return step
}
class Project {
var configurations = mutablelistof<Configuration>()
fun build(block: Configuration.() -> Unit) = Configuration().apply(block)
}
class Configuration {
var steps = mutablelistof<Step>()
}
open class Step {
final lateinit var name: String
var command: String = ""
}
open class CustomStep(): Step(){
var anotherCommand: String = ""
constructor(init: CustomStep.() -> Unit): this(){
// what does this do?
init()
}
}
fun Configuration.customstep(block: CustomStep.() -> Unit): Step {
// how is this constructor initialized
val step = CustomStep(block)
steps.add(step)
return step
}
具体来说,问题是关于 CustomStep
类是如何初始化的。它需要一个带有 CustomStep
as the reciever 的 lambda(这是正确的术语吗?)。
然后我在构造函数中调用 init()
,它根据传入的块初始化新创建的 CustomStep
。
我不确定初始化是如何工作的。或者更确切地说,这里使用了哪个特定的 Kotlin 语言功能。
如果我改为按照以下方式编写,这有什么不同?
open class CustomStep(): Step(){
var anotherCommand: String = ""
// no constructor
}
fun Configuration.customstep(block: CustomStep.() -> Unit): Step {
// use apply vs. passing in the block
val step = CustomStep().apply(block)
steps.add(step)
return step
}
谢谢
解决方法
init()
指的是参数 init: CustomStep.() -> Unit
:
constructor(init: CustomStep.() -> Unit): this(){
// vvvv ^^^^
init()
}
您只是在 this
上调用您传入的内容。 init
毕竟需要一个 CustomStep
作为接收者。与在 this
上调用某些内容时的大多数情况一样,可以省略 this
,这就是这里发生的情况。在 customStep
的情况下,您传入了 block
。
val step = CustomStep(block)
block
来自 main
:
{
name = "flang"
anotherCommand = "derp"
command = "1111"
}
您对 CustomStep().apply(block)
的选择也是一样的。调用您声明的辅助构造函数将首先调用无参数的主构造函数,因为您已声明为 : this()
,and is required。这与 CustomStep()
相同。然后两个版本都在 block
上调用 this
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。