与 anko 一样你可以这样写回调函数:
alert {
title = ""
message = ""
yesButton {
toast("Yes")
}
noButton {
toast("No")
}
}
如何创建这样的嵌套函数?我尝试像下面这样创建它,但似乎没有用。
class Test {
fun f1(function: () -> Unit) {}
fun f2(function: () -> Unit) {}
}
现在,如果我将它与扩展功能一起使用,
fun Context.temp(function: Test.() -> Unit) {
function.onSuccess() // doesn't work
}
从 Activity 调用这个:
temp {
onSuccess {
toast("Hello")
}
}
不起作用。我在这里仍然缺乏一些基本概念。有人可以在这里指导吗?
最佳答案
Kotlin DSL
Kotlin 非常适合编写自己的领域特定语言,也称为 type-safe builders .正如您所提到的,Anko 库是一个使用 DSL 的示例。您需要在这里了解的最重要的语言功能称为 "Function Literals with Receiver" ,您已经使用过:Test.() -> Unit
带有接收器的函数文字 - 基础
Kotlin 支持“带有接收器的函数文字”的概念。这使得在其主体中调用函数文字的接收者上的可见方法,而无需任何特定的限定符。这非常类似于扩展函数,也可以在扩展函数中访问接收器对象的成员。
一个简单的例子,也是 Kotlin 标准库中最酷的函数之一,是apply
:
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
。在 {}
(lambda 表达式)中作为参数传递的 block
不需要使用额外的限定符,只需调用 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")
}
}
}
关于android - 如何创建 Kotlin DSL - DSL 语法 Kotlin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46113833/