Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 244 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Modify "this" in extension function

#1
I want to write extension function that modifies "this", for example:

var a = false
a.toggle() // now a contains false

or

var a = 1
a.increment() // now a contains 2

Is it possible in Kotlin?

I can create extension function that returns modified value, but leaves "this" unmodified, but I want even more convenience! It looks like Swift can do that.
Reply

#2
You can not do this because it would require you to pass `a` to the function by reference. This is not possible in Kotlin. All function arguments in Kotlin are passed by value.

However, you could simulate the behaviour using the following construct. It's not very convenient though.

fun Boolean.toggle(getter: () -> Boolean, setter: (Boolean) -> Unit) {
setter(!getter())
}

var a = false

println(a) // prints false

a.toggle({a}, {a = it})
// Or a.toggle(a::get, a::set), but that isn't supported (yet?)

println(a) // prints true
Reply

#3
As any other parameter of a function, `this` is a reference to an object. On the other hand, `var a` is a reference to the same object too. So basically you have two references that point to the same instance:

var a = false
val this = a // a.toggle()

There is no manipulation you can do to one of those references and change the other. They are unrelated variables with the same value, nothing more.

What you can do (in theory) is to make the object itself changeable:

class MutableBoolean(val value: Boolean)

val a = MutableBoolean(false)
a.toggle()

fun MutableBoolean.toggle() {value = !value}
Reply

#4
References to variables aren't supported yet but you can create extension functions to use with [property references][1]:

fun KMutableProperty0<Boolean>.not() = set(get().not())
fun KMutableProperty0<Int>.inc() = set(get().inc())

var a = false
var b = 1

fun main(vararg args: String) {
::a.not()
// now `a` contains `true`
::b.inc()
// now `b` contains `2`
}

Or if you'd rather the extension functions return the new value along with setting it:

fun KMutableProperty0<Boolean>.not(): Boolean = get().not().apply(setter)
fun KMutableProperty0<Int>.inc(): Int = get().inc().apply(setter)

[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through