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 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
@renaudcerrato
renaudcerrato / snippet.kt
Last active February 19, 2019 12:40
Kotlin Lazy
class DeepThought {
val answer: String by lazy {
sleep(7_500_000, ChronoUnit.YEARS)
"42"
}
}
@renaudcerrato
renaudcerrato / snippet.kt
Last active February 19, 2019 12:29
Kotlin LateInit
class Presenter {
lateinit var view: View
fun setup(_view: View) {
// ensure the lateinit var hasn't been initialized
if(this::view.isInitialized) throw IllegalStateException()
view = _view
}
}