Getting access to an instance in a wrapper function - kotlin

In Kotlin I have this function to wrap a transaction:
fun wrapInTransaction(code: () -> Unit) {
realmInstance.beginTransaction();
code.invoke()
realmInstance.commitTransaction();
}
How can I get access to realmInstance in the invoked code?

The easy solution here is to make code a function with receiver:
fun wrapInTransaction(code: Realm.() -> Unit) {
realmInstance.beginTransaction();
realmInstance.code()
realmInstance.commitTransaction();
}
Inside a lambda which you pass as code you will be able to use this to reference the RealmInstance and to use its members directly as if inside a member function.
Calling realmInstance.code() is just calling code with passing realmInstance as a receiver to it.

The other answers correctly demonstrate how to pass the RealmInstance object to the lambda. In addition, you can make the whole function an extension function which makes the call site a bit prettier:
fun Realm.wrapInTransaction(code: Realm.() -> Unit) {
//this is implicit
beginTransaction();
code()
commitTransaction();
}
The call site will look like this:
Realm.getInstance(this).wrapInTransaction {
createObject(User.class)
}

Change the wrapInTransaction function to accept an extensions method on realmInstance like so:
fun wrapInTransaction(code:Realm.() -> Unit){
realmInstance.beginTransaction();
realmInstance.code()
realmInstance.commitTransaction();
}
Then you can use it like:
wrapInTransaction {
println("realm instance is $this, instanceId: $instanceId")
}
Where for the sake of the example the Realm looks like:
class Realm {
val instanceId = 42
fun beginTransaction() {
}
fun commitTransaction() {
}
}
The above technique is possible thanks to Kotlin's Function Literals with Receiver that make it possible to set the this instance (receiver) within lambda function body. It makes it easy to build type safe builders that reassemble ones from Groovy or Ruby.
This answer provides more samples on the technique.

Related

How does defining functions within functions work?

I am trying to get to know Kotlin through making a Ktor program, and was following the documentation when I noticed this:
fun Application.configureRouting() {
routing {
get("/") {
call.respondText("Hello World!")
}
}
}
How does the routing {} and get("/") {} work? What does it mean? Is routing and get a function being overridden within the Application.configureRouting() function?
I suppose you confused Kotlin's type-safe builders with local functions. It's possible to define a function inside another function (local function) which limits the scope in which we can access the child function.
Here's an example of a local function:
fun foo() {
fun bar() {
println("I'm within a local function.")
}
println("We can call bar only from foo.")
bar()
}
In case of type-safe builders (the routing function of your code), a part of the syntax that enabled this look and feel, is:
According to Kotlin convention, if the last parameter of a function is a function, then a lambda expression passed as the corresponding argument can be placed outside the parentheses.
When the only parameter of a function is of a lambda type, the parentheses can be omitted. Also, adding a receiver to a single lambda parameter will result in a behavior similar to the routing function that you mentioned. If my explanation is not sufficient, you can read more about type-safe builders from the official docs.

Can't understand specific kind of kotlin functions

For example:
private fun TextView.onEndDrawableClicked(onClicked: (view: TextView) -> Unit) {
this.setOnTouchListener { v, event ->
var hasConsumed = false
if (v is TextView) {
if (event.x >= v.width - v.totalPaddingRight) {
if (event.action == MotionEvent.ACTION_UP) {
onClicked(this)
}
hasConsumed = true
}
}
hasConsumed
}
}
In the example above we see extension function. I know what it is and can use use/create similar ones. But there's onClicked: (view: TextView) -> Unit in the example's parameters. What is this? Callback? I see this kind of parameters too often, but don't have any idea how to understand that. Does it reference to some lambda function? Can someone send me detailed manual/example of this kind of parameters/functions/whatever?
onClicked is a function that gets passed to onEndDrawableClicked. It's type is a functional type (view: TextView) -> Unit. That function, when called, expects one parameter view of type TextView and returns Unit.
So yes, it is a callback, passed as a lambda. It can be used like:
val textView: TextView = ...
textView.onEndDrawableClicked {
// code that should be executed when onClicked gets called.
}
Functions are first class citiziens in Kotlin. Meaning you can store function in variables and pass them to other functions. Or let functions return functions.
When to use?
You can pass a function whenever you want to pass behavior (rather than state) to or from your functions. So the caller can decide what to do in those cases. That allows you to write highly flexible code / API's.
Alternatively you could create an interface with one ore many functions to be called and pass an instance of that interface to your function.

Explanation needed for Kotlin receiver function use for DSL

I'm trying to understand the following code (source).
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
}
What I really cannot grasp is the line
html.init() // pass the receiver object to the lambda
What is happening here?
Can somebody please explain in simple words what is going on here?
First, let's make this example a little easier and see what the problem is then.
We could build the html function like this:
fun html(init: (HTML) -> Unit): HTML {
val html = HTML()
init(html)
return html
}
This would be easier to grasp (at first), because we are just passing a usual one-parameter lambda to the html function.
But now the call-site is not builder like:
html { it: HTML -> // just for clarity
it.body() // not very nice
}
Wouldn't it be nice if we could invoke body() inside html without it? That's possible! All we need is a lambda with receiver.
fun html(init: HTML.() -> Unit): HTML { // <-- only this changed
val html = HTML()
init(html)
return html
}
See how html is passed as an argument to init like before?
Of course, we can invoke it like this too: html.init() as shown in the example. The instance of HTML becomes this inside the block of the lambda.
Now, we can do this:
html {
this.body()
}
Since this can be omitted, we arrive here:
html {
body()
}
So, in the end lambdas with receivers make the code more concise and allow us to use a nice builder syntax.
Here is step by step explaination:
1. Creation of function, receiver type lambda.
fun html(init: HTML.() -> Unit): HTML {
here function html accept a parameter init of type HTML.() -> Unit i.e. it indicated that it is a receiver of HTML and can only be called with help of a real HTML object. And : HTML indicates that the function obviously returns HTML object.
2. call of init at html
html.init()
Here init() function is called as a receiver of HTML by a real HTML object.
Alright enough of formal talking, Here is what a receiver is:
So if you remember extension function defined as fun A.myFun(...): ReturnType {}, in that case you get a variable this which act as an instance of type A it was called on.
Similarly receiver lambda gives you a this variable inside that,
In a particular example:
class A {
fun thisCanBeCalledByAInstance() {
println("I've got called")
}
}
fun main() {
val receiver: A.() -> Unit = { // this: A
thisCanBeCalledByAInstance() // prints: I've got called
// or traditional way this.thisCanBeCalledByAInstance()
}
val a: A = A()
a.receiver()
}
Here you were able to call the method(function) from the instance of A even if it was lambda because it was a receiver.
PS: For simple langauge you can think html.init() as init(html) but html is not a parameter but instead works as this vaiable inside the lambda
This is why you were able to call body() on that lambda, because implicitly you were calling this.body() and this has came from html.init()'s html object.

Kotlin Extension Function on Observable<T>.subscribe does not work

I'm trying to write an extension function for Observable.subscribe which automatically logs errors.
fun <T> Observable<T>.subscribeAndLogE(onNext: Consumer<in T>): Disposable =
subscribe(onNext, ErrorConsumer())
The ErrorConsumer works and presumably logs the error, but subscribeAndLogE does not accept lambdas like .subscribe() does.
observer.subscribe {
//works
}
observer.subscribeAndLogE {
//does not work
}
It says:
With that OnboardingScreen being whichever value T would normally be.
I don't see the original Consumer<in T> in Observable doing anything special to accept lambdas. What am I doing wrong here?
You are passing a parameter of type Consumer to the function. You need to pass a function for the lambda syntax to work. This would work the way you want to:
fun <T> Observable<T>.subscribeAndLogE(onNext: (it : T) -> Unit): Disposable =
subscribe({ onNext(it) },{ throwable -> Log(throwable) })
and use it like so:
observer.subscribeAndLogE {
//works
}

Wrong "this" being used in nested closures

I'm trying to keep this minimal, but let me know if I'm being too minimal.
Suppose you have a class hierarchy like this, designed for generating HTML (inspired by the Kotlin tutorial; semi-pseudocode follows):
class Tag {
protected val children = arrayListOf<Tag>()
operator fun String.unaryPlus() = children.add(Text(this))
}
class TagWithChildren : Tag() {
fun head(init: Head.() -> Unit) = initializeTag(Head(), init)
fun script(init: Script.() -> Unit) = initializeTag(Script(), init)
fun <T : Tag> initializeTag(tag: T, init: T.() -> Unit): T {
tag.init()
children.add(tag)
return tag
}
}
class Head : TagWithChildren()
class Script : Tag()
class Text(val str: Text) : Tag()
Notice that Head has head and script methods while Script doesn't.
Now you can construct a template that looks like this:
head {
script {
+"alert('hi');"
}
}
Which works great! However, if the block passed to script tries to call methods that aren't available on Script, it can call the method on Head instead. For example,
head {
script {
script {
+"alert('hi');"
}
}
}
not only isn't a compile error, it's actually equivalent to
head {
script {
}
script {
+"alert('hi');"
}
}
which is super confusing, from a template author's perspective.
Is there any way to prevent method lookups from traveling up the scope like that? I only want it to look at the innermost scope.
UPDATE 11/24/2016:
Kotlin 1.1-M03 has introduced scope control, which I believe solves exactly this problem. https://blog.jetbrains.com/kotlin/2016/11/kotlin-1-1-m03-is-here/
The current behavior is intentional. Code in a lambda has access to receivers of all enclosing scopes. It is possible that a future version of Kotlin will add a modifier that will restrict a lambda with receiver to calling methods on that receiver only and not the enclosing scopes, but in the current version there's no way to change that behavior.
As a workaround, I can have it fail at runtime if I change the classes to look like this:
open class Tag {
operator fun String.unaryPlus()
// pulled up from TagWithChildren, call protected method
fun head(init: Head.() -> Unit) = addChild(Head())
fun script(init: Script.() -> Unit) = addChild(Head())
// throws in Tag
open protected fun addChild(t: Tag) = throw IllegalArgumentException()
}
class TagWithChildren : Tag() {
// overridden to not throw in subclass
protected override fun addChild(t: Tag) = children.add(t)
}
This way, every Tag has the builder methods (solving the scoping problem), but actually calling them may result in a runtime failure.