Share
Embed
Transcript
98 slides
-
fun buildString( builderAction: (StringBuilder) -> Unit ): String {a }b
fun main(args: Array<String>) {c }e -
fun buildString( builderAction: (StringBuilder) -> Unit ): String {a val
sb = StringBuilder() builderAction(sb) return sb.toString() }b fun main(args: Array<String>) {c }e -
fun buildString( builderAction: (StringBuilder) -> Unit ): String {a val
sb = StringBuilder() builderAction(sb) return sb.toString() }b fun main(args: Array<String>) {c val s = buildString({ stringBuilder -> stringBuilder.append("Hello, ") stringBuilder.append("World!") })d }e -
fun buildString( builderAction: (StringBuilder) -> Unit ): String {a val
sb = StringBuilder() builderAction(sb) return sb.toString() }b fun main(args: Array<String>) {c val s = buildString { stringBuilder -> stringBuilder.append("Hello, ") stringBuilder.append("World!") }d }e -
fun buildString( builderAction: (StringBuilder) -> Unit ): String {a val
sb = StringBuilder() builderAction(sb) return sb.toString() }b fun main(args: Array<String>) {c val s = buildString { it.append("Hello, ") it.append("World!") }d }e -
fun buildString( builderAction: (StringBuilder) -> Unit ): String {a val
sb = StringBuilder() builderAction(sb) return sb.toString() }b fun main(args: Array<String>) {c val s = buildString { append("Hello, ") append("World!") }d }e -
fun buildString( builderAction: StringBuilder.() -> Unit ): String {a val
sb = StringBuilder() builderAction(sb) return sb.toString() }b fun main(args: Array<String>) {c val s = buildString { append("Hello, ") append("World!") }d }e -
fun buildString( builderAction: StringBuilder.() -> Unit ): String {a val
sb = StringBuilder() sb.builderAction() return sb.toString() }b fun main(args: Array<String>) {c val s = buildString { append("Hello, ") append("World!") }d }e -
fun buildString( builderAction: StringBuilder.() -> Unit ): String {a val
sb = StringBuilder() sb.builderAction() return sb.toString() }b fun main(args: Array<String>) {c val s = buildString { append("Hello, ") append("World!") }d }e -
fun buildString( builderAction: StringBuilder.() -> Unit ): String {a val
sb = StringBuilder() sb.builderAction() return sb.toString() }b fun main(args: Array<String>) {c val s = buildString { this.append("Hello, ") append("World!") }d }e -
fun <T, R> with( receiver: T, block: T.() -> R
): R { return receiver.block() } -
interface BinaryNumber object Zero : BinaryNumber object One : BinaryNumber
fun BinaryNumber.toInt():Int {a return when (this) {b is One -> 1 is Zero -> 0 else -> throw IllegalStateException() }c }d -
interface BinaryNumber object Zero : BinaryNumber object One : BinaryNumber
fun BinaryNumber.toInt():Int {a return when (this) {b is One -> 1 is Zero -> 0 }c }d -
interface BinaryNumber object Zero : BinaryNumber object One : BinaryNumber
fun BinaryNumber.toInt():Int {a return when (this) {b is One -> 1 is Zero -> 0 else -> throw IllegalStateException() }c }d -
interface BinaryNumber object Zero : BinaryNumber object One : BinaryNumber
class Magic() : BinaryNumber() { fun abracadabra() { // ... } } fun BinaryNumber.toInt():Int {a return when (this) {b is One -> 1 is Zero -> 0 else -> throw IllegalStateException() }c }d -
interface BinaryNumber object Zero : BinaryNumber object One : BinaryNumber
fun BinaryNumber.toInt():Int {a return when (this) {b is One -> 1 is Zero -> 0 else -> throw IllegalStateException() }c }d -
sealed class BinaryNumber object Zero : BinaryNumber() object One :
BinaryNumber() fun BinaryNumber.toInt():Int {a return when (this) {b is One -> 1 is Zero -> 0 else -> throw IllegalStateException() }c }d -
sealed class BinaryNumber object Zero : BinaryNumber() object One :
BinaryNumber() fun BinaryNumber.toInt():Int {a return when (this) {b is One -> 1 is Zero -> 0 }c }d -
sealed class BinaryNumber object Zero : BinaryNumber() object One :
BinaryNumber() class Magic() : BinaryNumber() {z fun abracadabra() {w // ... }t }r fun BinaryNumber.toInt():Int {a return when (this) {b is One -> 1 is Zero -> 0 }c }d -
sealed class BinaryNumber object Zero : BinaryNumber() object One :
BinaryNumber() class Magic() : BinaryNumber() {z fun abracadabra() {w // ... }t }r fun BinaryNumber.toInt():Int {a return when (this) {b is One -> 1 is Zero -> 0 }c }d -
sealed class BinaryNumber object Zero : BinaryNumber() object One :
BinaryNumber() class Magic() : BinaryNumber() {z fun abracadabra() {w // ... }t }r fun BinaryNumber.toInt():Int {a return when (this) {b is One -> 1 is Zero -> 0 is Magic -> this.abracadabra() }c }d -
sealed class BinaryNumber object Zero : BinaryNumber() object One :
BinaryNumber() class Magic() : BinaryNumber() { fun abracadabra() { // ... } } fun BinaryNumber.toInt():Int {a return when (this) {b is One -> 1 is Zero -> 0 is Magic -> this.abracadabra() }c }d -
fun requireNotNull(obj: Any?) {a if (obj == null) throw IllegalArgumentException()
}d fun foo(s: String?) {e requireNotNull(s) s.length }f -
fun requireNotNull(obj: Any?) {a contract { returns() implies (obj !=
null) }c if (obj == null) throw IllegalArgumentException() }d fun foo(s: String?) {e requireNotNull(s) s.length }f -
people // List<People> .asSequence() // Sequence<People> .map(People::name) // Sequence<String> .filter
{ it.startsWith("J") } // Sequence<String> -
people // List<People> .asSequence() // Sequence<People> .map(People::name) // Sequence<String> .filter
{ it.startsWith("J") } // Sequence<String> .toList() // List<People> -
people .map(People::name) .find { it.startsWith("J") } And Car Jes Wil
Pir A C J W P A C J -
fun sumUp(items: Collection<*>) {a if (items is List<Int>) {d println(ints.sum())
}b }c val strings: List<String> = listOf("a", "b") sumUp(strings) -
fun sumUp(items: Collection<*>) {a if (items is List<Int>) {d println(ints.sum())
}b }c val strings: List<String> = listOf("a", "b") sumUp(strings) -
fun sumUp(items: Collection<*>) {a val ints = items as? List<Int>
?: return println("We got a list of ints") println(ints.sum()) }c val strings: List<String> = listOf("a", "b") sumUp(strings) -
fun sumUp(items: Collection<*>) {a val ints = items as? List<Int>
?: return println("We got a list of ints") println(ints.sum()) }c val strings: List<String> = listOf("a", "b") sumUp(strings) ----- We got a list of ints Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Number -
fun sumUp(items: Collection<*>) {a val ints = items as? List<Int>
?: return println("We got a list of ints") println(ints.sum()) }c val strings: List<String> = listOf("a", "b") sumUp(strings) -
fun sumUp(items: Collection<Int>) {a val ints = items as? List<Int>
?: return println("We got a list of ints") println(ints.sum()) }c val strings: List<String> = listOf("a", "b") sumUp(strings) -
fun <T> Iterable<*>.filterIsInstance(): List<T> {a val destination = mutableListOf<T>() for
(element in this) {c if (element is T) {d destination.add(element) }p }e return destination }f -
fun <T> Iterable<*>.filterIsInstance(): List<T> {a val destination = mutableListOf<T>() for
(element in this) {c if (element is T) {d destination.add(element) }p }e return destination }f listOf("a", 1, 0x0, 2L, 'u') .filterIsInstance<String>() -
fun <T> Iterable<*>.filterIsInstance(): List<T> {a val destination = mutableListOf<T>() for
(element in this) {c if (element is T) {d destination.add(element) }p }e return destination }f listOf("a", 1, 0x0, 2L, 'u') .filterIsInstance<Int>() -
fun <T> Iterable<*>.filterIsInstance(): List<T> {a val destination = mutableListOf<T>() for
(element in this) {c if (element is T) {d destination.add(element) }p }e return destination }f listOf("a", 1, 0x0, 2L, 'u') .filterIsInstance<Char>() -
fun <T> Iterable<*>.filterIsInstance(): List<T> {a val destination = mutableListOf<T>() for
(element in this) {c if (element is T) {d destination.add(element) }p }e return destination }f -
fun <T> Iterable<*>.filterIsInstance(): List<T> {a val destination = mutableListOf<T>() for
(element in this) {c if (element is T) {d destination.add(element) }p }e return destination }f -
inline fun <T> Iterable<*>.filterIsInstance(): List<T> {a val destination = mutableListOf<T>()
for (element in this) {c if (element is T) {d destination.add(element) }p }e return destination }f -
inline fun <reified T> Iterable<*>.filterIsInstance(): List val destination = mutableListOf<T>()
for (element in this) {c if (element is T) {d destination.add(element) }p }e return destination }f -
inline fun <reified T> Iterable<*>.filterIsInstance(): List val destination = mutableListOf<T>()
for (element in this) {c if (element is T) {d destination.add(element) }p }e return destination }f -
inline fun <reified T> Iterable<*>.filterIsInstance(): List val destination = mutableListOf<T>()
for (element in this) {c if (element is T) {d destination.add(element) }p }e return destination }f listOf("a", 1, 0x0, 2L, 'u') .filterIsInstance<String>() -
inline fun <reified T> Moshi.adapter(): JsonAdapter<T> {a return this.adapter(T::class.java) }b
val moshi = Moshi.Builder().build() moshi.adapter<String>() -
If you really want to grow as a Klass, you've
got to learn to delegate -
class CountingSet<T>( val innerSet: MutableCollection<T> = HashSet() ) : MutableCollection<T>
{abcdefgh var objectsAdded = 0 }t -
class CountingSet<T>( val innerSet: MutableCollection<T> = HashSet() ) : MutableCollection<T>
{abcdefgh var objectsAdded = 0 override fun add(element: T): Boolean {w objectsAdded++ return innerSet.add(element) }p override fun addAll(c: Collection<T>): Boolean {o objectsAdded += c.size return innerSet.addAll(c) }k }t -
class CountingSet<T>( val innerSet: MutableCollection<T> = HashSet() ) : MutableCollection<T>
{abcdefgh var objectsAdded = 0 override fun add(element: T): Boolean {w objectsAdded++ return innerSet.add(element) }p override fun addAll(c: Collection<T>): Boolean {o objectsAdded += c.size return innerSet.addAll(c) }k }t -
class CountingSet<T>( val innerSet: MutableCollection<T> = HashSet() ) : MutableCollection<T>
{abcdefgh var objectsAdded = 0 override fun add(element: T): Boolean {w objectsAdded++ return innerSet.add(element) }p override fun addAll(c: Collection<T>): Boolean {o objectsAdded += c.size return innerSet.addAll(c) }k override val size: Int get() = innerSet.size override fun contains(element: T): Boolean { return innerSet.contains(element) } override fun containsAll(elements: Collection<T>): Boolean { return innerSet.containsAll(elements) } override fun isEmpty(): Boolean { return innerSet.isEmpty() } override fun clear() { innerSet.clear() } override fun iterator(): MutableIterator<T> { return innerSet.iterator() } override fun remove(element: T): Boolean { return innerSet.remove(element) } override fun removeAll(elements: Collection<T>): Boolean { return innerSet.removeAll(elements) } override fun retainAll(elements: Collection<T>): Boolean { return innerSet.retainAll(elements) } }t -
class CountingSet<T>( val innerSet: MutableCollection<T> = HashSet() ) : MutableCollection<T>
{abcdefgh var objectsAdded = 0 override fun add(element: T): Boolean {w objectsAdded++ return innerSet.add(element) }p override fun addAll(c: Collection<T>): Boolean {o objectsAdded += c.size return innerSet.addAll(c) }k }t -
class CountingSet<T>( val innerSet: MutableCollection<T> = HashSet() ) : MutableCollection<T>
by innerSet {abcdefgh var objectsAdded = 0 override fun add(element: T): Boolean {w objectsAdded++ return innerSet.add(element) }p override fun addAll(c: Collection<T>): Boolean {o objectsAdded += c.size return innerSet.addAll(c) }k }t -
class CountingSet<T>( val innerSet: MutableCollection<T> = HashSet() ) : MutableCollection<T>
by innerSet {abcdefgh var objectsAdded = 0 override fun add(element: T): Boolean {w objectsAdded++ return innerSet.add(element) }p override fun addAll(c: Collection<T>): Boolean {o objectsAdded += c.size return innerSet.addAll(c) }k }t -
class Foo { val data: Int by lazy { computeStuff()
} } • lazy • observable • vetoable • notNull -
assertThat(toString(taco)).isEqualTo("" + "package com.squareup.tacos\n" + "\n" + "import kotlin.String\n" +
"\n" + "class Taco {\n" + " final override fun toString(): String = \"taco\"\n" + "}\n" + "")c -
assertThat(toString(taco)).isEqualTo(""" package com.squareup.tacos import kotlin.String class Taco { final override
fun toString(): String = "taco" }z """)c -
assertThat(toString(taco)).isEqualTo(""" package com.squareup.tacos import kotlin.String class Taco { final override
fun toString(): String = "taco" }z """)c -
assertThat(toString(taco)).isEqualTo(""" |package com.squareup.tacos | |import kotlin.String | |class Taco {
| final override fun toString(): String = "taco" |}z |""")c -
assertThat(toString(taco)).isEqualTo(""" |package com.squareup.tacos | |import kotlin.String | |class Taco {
| final override fun toString(): String = "taco" |} |""".trimMargin())c -
class SomeList {a @Deprecated("Use removeAt(index) instead.")h fun remove(index: Int) {b
}c fun removeAt(index: Int) {d }e }f -
class SomeList {a @Deprecated("Use removeAt(index) instead.", ReplaceWith("removeAt(index)"))h fun remove(index: Int)
{b }c fun removeAt(index: Int) {d }e }f -
class SomeList {a @Deprecated("Use removeAt(index) instead.", ReplaceWith("removeAt(index)"))h fun remove(index: Int)
{b }c fun removeAt(index: Int) {d }e }f