kir*_*403 11 android kotlin anko kotlin-extension
和anko一样,你可以写这样的回调函数:
alert {
title = ""
message = ""
yesButton {
toast("Yes")
}
noButton {
toast("No")
}
}
Run Code Online (Sandbox Code Playgroud)
我该如何创建这样的嵌套函数?我尝试像下面那样创建它,但似乎没有工作.
class Test {
fun f1(function: () -> Unit) {}
fun f2(function: () -> Unit) {}
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我用扩展功能,
fun Context.temp(function: Test.() -> Unit) {
function.onSuccess() // doesn't work
}
Run Code Online (Sandbox Code Playgroud)
从活动中调用此内容:
temp {
onSuccess {
toast("Hello")
}
}
Run Code Online (Sandbox Code Playgroud)
不行.我在这里仍然缺乏一些基本概念.谁能在这里指导?
s1m*_*nw1 12
Kotlin非常适合编写自己的Domain Specific Languages,也称为类型安全构建器.如您所述,Anko库是一个使用DSL的示例.您需要了解的最重要的语言功能称为"带接收器的函数文字",您已经使用了它:Test.() -> Unit
Kotlin支持"带接收器的函数文字"的概念.这样就可以在函数文本的接收器上调用可见方法,而不需要任何特定的限定符.这与扩展函数非常相似,在扩展函数中也可以访问扩展中的接收者对象的成员.
一个简单的例子,也是Kotlin标准库中最酷的函数之一,是apply:
public inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this }
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,这种带接收器的函数文字block在此处作为参数.这block只是执行,并T返回接收器(它的实例).在行动中,这看起来如下:
val text: String = StringBuilder("Hello ").apply {
append("Kotliner")
append("! ")
append("How are you doing?")
}.toString()
Run Code Online (Sandbox Code Playgroud)
A StringBuilder用作接收器并apply在其上调用.的block,如在参数传递{}(lambda表达式),不需要使用额外的限定符和简单地调用append,的可视方法StringBuilder多次.
如果你看一下这个从文档中获取的例子,你会看到这个:
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
}
Run Code Online (Sandbox Code Playgroud)
该html()函数期望这样的函数文字与接收器HTML作为接收器.在函数体中,您可以看到它的使用方式:HTML创建一个实例并在其init上调用它.
这样一个高阶函数的调用者期望函数文字与接收器(如html())你可以使用任何可见的HTML函数和属性而无需额外的限定符(this例如),你可以在调用中看到:
html { // lambda with receiver begins here
body() // calling a method on the receiver object
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个你想要的简单例子:
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")
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
812 次 |
| 最近记录: |