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 March 15, 2019 18:50
Kotlin Infix Functions (Standard Library)
// until and step are infix functions
for(i in 0 until 100 step 2) println("$i")
// equivalent to:
for(i in 0.until(100).step(2)) println("$i")
// downTo is an infix function
for(i in 100 downTo 0) println("$i")
// shl, and, xor are infix functions
val i = (0x65acf9 shl 6) and 0x55 xor 0x80
@renaudcerrato
renaudcerrato / snippet.kt
Last active February 23, 2019 06:22
Kotlin Infix Functions
class Price(val value: Double, val currency: Currency)
infix fun Int.euro(cents: Int): Price {
return Price(toDouble() + cents / 100.0, Currency.EURO)
}
val price = 1 euro 42
// equivalent to:
val price = 1.euro(42)
@renaudcerrato
renaudcerrato / snippet.kt
Last active February 23, 2019 06:52
Kotlin Anonymous Objects
val listener = object: OnMenuClickListener, OnMenuExpandListener {
override fun onMenuClick(item: Menu) { ... }
override fun onMenuExpand(item: Menu) { ... }
}
menu.setOnClickListener(listener)
menu.setOnExpandListener(listener)
@renaudcerrato
renaudcerrato / snippet.kt
Created February 22, 2019 13:57
Kotlin Companion Object
class Person private constructor(val name: String, val age: Int) {
companion object Factory {
fun create(name: String, age: Int) = Person(name, age)
}
}
// members of the companion object can be called using the class name as qualifier
val bob = Person.create("Bob", 21)
@renaudcerrato
renaudcerrato / snippet.kt
Last active February 22, 2019 14:38
Kotlin Object Singleton
// object class as a singleton
object Log {
val prop = 42
fun debug(msg: String) { ... }
fun info(msg: String) { ... }
fun warn(msg: String) { ... }
fun error(msg: String) { ... }
}
// object classes can inherit from classes and interfaces
@renaudcerrato
renaudcerrato / snippet.kt
Last active February 22, 2019 09:52
Kotlin Nested Classes
class A {
// nested class (equivalent to static in Java)
class B {
// A can't see private members of nested classes
private val invisible = 42
}
// inner class
inner class C {
// A can't see private members of inner classes
@renaudcerrato
renaudcerrato / snippet.kt
Last active February 22, 2019 08:42
Kotlin Operators Example
class Point(val x: Double, val y: Double) {
// addition operator
operator fun plus(other: Point): Point {
return Point(x + other.x, y + other.y)
}
// overload using Double
operator fun plus(value: Double): Point {
return Point(x + value, y + value)
}
@renaudcerrato
renaudcerrato / snippet.kt
Last active March 15, 2019 20:20
Kotlin InterfaceDelegation
interface View {
fun click()
fun toggle()
}
class ViewImpl: View {
override fun click() { println("click!") }
override fun toggle() { println("toggle!") }
}
@renaudcerrato
renaudcerrato / snippet.kt
Last active April 2, 2023 13:55
Kotlin Interfaces
interface A {
val a: Int // abstract
val b: Int // abstract
fun foo() // abstract
fun bar() {
// optionnal body
}
}
interface B { ... }
@renaudcerrato
renaudcerrato / snippet.kt
Last active February 21, 2019 07:32
Kotlin Access Modifiers Classes
// final public class (default)
class A
// final private class (only visible in the file)
private class B
// final internal class (only visible in the module)
internal class C
// abstract public class