This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Capitalizer { | |
var value: String? = null | |
operator fun getValue(thisRef: Any?, property: KProperty<*>): String? { | |
return value | |
} | |
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String?) { | |
this.value = value?.capitalize() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// custom getter on an immutable property (no setter allowed) | |
class Person(val name: String, val birthday: LocalDate) { | |
val age: Long | |
get() = birthday.until(LocalDate.now(), ChronoUnit.YEARS) | |
} | |
// custom setter | |
class Contact { | |
var firstName: String? = null | |
set(value) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person(val name: String) { | |
// secondary constructors must delegate to the primary constructor | |
constructor(name: String, parent: Person) : this(name) { | |
parent.children.add(this) | |
} | |
init { | |
println("I'm called before the secondary constructor") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Primary constructor declaration | |
class Person constructor(name: String) { ... } | |
// The constructor keyword can be omitted if there's | |
// no visibility modifiers nor any annotations | |
class Person(name: String) { ... } | |
// Property initialization | |
class Person(_firstName: String, _lastName: String) { | |
val firstName = _firstName.capitalize() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
when(x) { | |
42 -> print("the answer") | |
0xbadbabe, 0xbadface -> print("l33t s534k!") | |
is String -> print("$[x.toUpperCase()]") | |
in 0..10 -> print("0 ≤ x ≤ 10") | |
!in 6..9 -> print("x < 6 and x > 9") | |
parseInt("0xcafe") -> print("no sugar please") | |
else -> print("?") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val s = with(StringBuilder()) { | |
append("Hello!") | |
append("How are you?") | |
toString() // implicit return value | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun buildString(action: StringBuilder.() -> Unit) : String { | |
val sb = StringBuilder() | |
sb.action() | |
return sb.toString() | |
} | |
// inside the lambda, `this` refer to the StringBuilder | |
val s = buildString { | |
append("Hello!") | |
append("How are you?") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun hello(vararg values: String) { | |
for(value in values) println("Hello $value!") | |
} | |
hello("Kotlin", "Java", "Groovy") | |
// calls using the spread operator | |
val names = arrayOf("Java", "Groovy") | |
hello(*names) | |
hello("Kotlin", *names) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun join(strings: Collection<String>, separator: String = " ", | |
prefix: String = "", postfix: String = "") : String | |
{ | |
val result = StringBuilder(prefix) | |
for((index, element) in strings.withIndex()) { | |
if(index > 0) result.append(separator) | |
result.append(element) | |
} | |
result.append(postfix) | |
return result.toString() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// block body | |
fun sum(a: Int, b: Int) : Int { | |
return a + b | |
} | |
// expression body, inferred return type | |
fun sum(a: Int, b: Int) = a + b | |
// variable of a function type | |
val foo: (Int, Int) -> Int = sum |