Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
data class Config( var host: String = "localhost", var port: Int = 8080, var debug: Boolean = false ) fun main() { // apply — configures an object, returns the object val config = Config().apply { host = "example.com" port = 443 debug = true } println(config) // let — transforms a nullable value, returns lambda result val name: String? = " Kotlin " val length = name?.let { val trimmed = it.trim() println("Trimmed: '$trimmed'") trimmed.length } println("Length: $length") // run — execute block, return result; also used on an object val summary = config.run { "Connecting to $host:$port (debug=$debug)" } println(summary) // also — side effect, returns the original object val numbers = mutableListOf(3, 1, 4, 1, 5) .also { println("Before sort: $it") } .also { it.sort() } .also { println("After sort: $it") } // with — group calls (not an extension, receiver = this) val message = with(StringBuilder()) { append("Hello") append(", ") append("Kotlin!") toString() } println(message) }
Result
Open