As with anko you can write callback functions like this:
alert {
title = ""
message = ""
yesButton {
toast("Yes")
}
noButton {
toast("No")
}
}
How can I create a nested functions like that? I tried creating it like below but doesn't seem to be working.
class Test {
fun f1(function: () -> Unit) {}
fun f2(function: () -> Unit) {}
}
Now, if I use this with extension function,
fun Context.temp(function: Test.() -> Unit) {
function.onSuccess() // doesn't work
}
Calling this from Activity:
temp {
onSuccess {
toast("Hello")
}
}
Doesn't work. I am still lacking some basic concepts here. Can anyone guide here?
Kotlin DSLs
Kotlin is great for writing your own Domain Specific Languages, also called type-safe builders. As you mentioned, the Anko library is an example making use of DSLs. The most important language feature you need to understand here is called "Function Literals with Receiver", which you made use of already:
Test.() -> Unit
Function Literals with Receiver - Basics
Kotlin supports the concept of “function literals with receivers”. This enables calling visible methods on the receiver of the function literal in its body without any specific qualifiers. This is very similar to extension functions, in which it’s also possible to access members of the receiver object inside the extension.
A simple example, also one of the coolest functions in the Kotlin standard library, is
apply
:As you can see, such a function literal with receiver is taken as an argument
block
here. Thisblock
is simply executed and the receiver (which is an instance ofT
) is returned. In action this looks as follows:A
StringBuilder
is used as the receiver andapply
is invoked on it. Theblock
, passed as an argument in{}
(lambda expression), does not need to use additional qualifiers and simply callsappend
, a visible method ofStringBuilder
multiple times.Function Literals with Receiver - in DSL
If you look at this example, taken from the documentation, you see this in action:
The
html()
function expects such a function literal with receiver withHTML
as the receiver. In the function body you can see how it is used: an instance ofHTML
is created and theinit
is called on it.Benefit
The caller of such an higher-order function expecting a function literal with receiver (like
html()
) you can use any visibleHTML
function and property without additional qualifiers (likethis
e.g.), as you can see in the call:Your Example
I created a simple example of what you wanted to have: