Kotlin DSL
Kotlin 非常适合编写自己的领域特定语言,也称为type-safe builders。正如您所提到的,Anko 库是一个使用 DSL 的示例。您需要在这里了解的最重要的语言功能称为"Function Literals with Receiver",您已经使用过它:Test.() -> Unit
带有接收器的函数文字 - 基础
Kotlin 支持“带有接收器的函数文字”的概念。这允许在其主体中调用函数文字的接收器上的可见方法,而无需任何特定的限定符。这非常类似于扩展函数,也可以在扩展函数中访问接收器对象的成员。
一个简单的例子,也是 Kotlin 标准库中最酷的函数之一,isapply:
public inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this }
如你所见,这样一个带有接收器的函数字面量在这里被当作参数block。这个block 被简单地执行并返回接收者(它是T 的一个实例)。实际情况如下:
val text: String = StringBuilder("Hello ").apply {
append("Kotliner")
append("! ")
append("How are you doing?")
}.toString()
StringBuilder 用作接收器,并在其上调用 apply。 block,作为{}(lambda表达式)中的参数传递,不需要使用额外的限定符,只需多次调用append,StringBuilder的可见方法。
带有接收器的函数文字 - 在 DSL 中
如果您查看此示例,取自文档,您会看到它的实际效果:
class HTML {
fun body() { ... }
}
fun html(init: HTML.() -> Unit): HTML {
val html = HTML() // create the receiver object
html.init() // pass the receiver object to the lambda
return html
}
html { // lambda with receiver begins here
body() // calling a method on the receiver object
}
html() 函数需要这样一个函数字面量,其中接收者为 HTML 作为接收者。在函数体中,您可以看到它是如何使用的:创建HTML 的实例并在其上调用init。
好处
这样一个高阶函数的调用者期望一个带有接收者的函数字面量(如html()),您可以使用任何可见的HTML函数和属性而无需额外的限定符(如this例如),正如您在通话中看到的那样:
html { // lambda with receiver begins here
body() // calling a method on the receiver object
}
你的例子
我创建了一个您想要的简单示例:
class Context {
fun onSuccess(function: OnSuccessAction.() -> Unit) {
OnSuccessAction().function();
}
class OnSuccessAction {
fun toast(s: String) {
println("I'm successful <3: $s")
}
}
}
fun temp(function: Context.() -> Unit) {
Context().function()
}
fun main(args: Array<String>) {
temp {
onSuccess {
toast("Hello")
}
}
}