Skip to content

Instantly share code, notes, and snippets.

View renaudcerrato's full-sized avatar

Renaud Cerrato renaudcerrato

View GitHub Profile
@renaudcerrato
renaudcerrato / snippet.kt
Last active February 19, 2019 11:42
Kotlin Delegate
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()
}
@renaudcerrato
renaudcerrato / snippet.kt
Last active February 19, 2019 11:44
Kotlin Properties Getter/Setter
// 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) {
@renaudcerrato
renaudcerrato / snippet.kt
Last active February 19, 2019 09:05
Kotlin Secondary Constructors
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")
}
}
@renaudcerrato
renaudcerrato / snippet.kt
Last active February 19, 2019 09:11
Kotlin Constructors
// 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()
@renaudcerrato
renaudcerrato / snippet.kt
Created February 9, 2019 09:14
when examples 2
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("?")
}
@renaudcerrato
renaudcerrato / snippet.kt
Last active February 9, 2019 08:06
Kotlin with/apply
val s = with(StringBuilder()) {
append("Hello!")
append("How are you?")
toString() // implicit return value
}
@renaudcerrato
renaudcerrato / snippet.kt
Last active February 9, 2019 07:52
Lambda with Receiver
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?")
@renaudcerrato
renaudcerrato / snippet.kt
Last active February 8, 2019 08:33
Kotlin vararg
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)
@renaudcerrato
renaudcerrato / snippet.kt
Last active February 7, 2019 07:30
Kotlin Functions, advanced
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()
@renaudcerrato
renaudcerrato / snippet.kt
Last active February 9, 2019 08:14
Kotlin Functions (simple)
// 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