Hello, can i define a lambda that accepts a mutab...
# announcements
o
Hello, can i define a lambda that accepts a mutable variable as parameter?
d
You want to be able to modify a variable that's passed into the lambda from the lambda?
o
yes
Copy code
val bbToString = { bb: ByteBuffer, d: String -> d = String(bb.array(), Charset.forName("US-ASCII")) }
d
You can't do this in Kotlin
o
ok then
d
If you must return multiple values, you can use
Pair<>
or a data class
You can also modify any variables in the scope of the lambda, of course
o
ok gotcha
s
@otakusenpai You could pass by reference via AtomicReference or use StringBuilder etc
Copy code
val lambda = { s : AtomicReference<String> ->
    s.set( "updated string" )
}

fun main( argv : Array<String> ) {
    val string = AtomicReference<String> ("initial value")

    println( string )

    lambda( string )

    println( string )

}
Here the reference is passed by value but you are working with what is being referred to