Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin ist auch eine Insel

Lovis
September 29, 2016

Kotlin ist auch eine Insel

From my talk at Code Talks 2016 (https://www.codetalks.de/):
Kotlin and some Extension Function Magic for beginners. (german)

#CodeTalksHH

Lovis

September 29, 2016
Tweet

More Decks by Lovis

Other Decks in Programming

Transcript

  1. Kotlin ist auch eine Sprache • Entwickelt von JetBrains •

    Concise & Expressive • Sicher • Pragmatisch • 100% Interoperabel mit Java und den Java Apis • Für Java Entwickler leicht zu erlernen Lovis Möller @lovisbrot #codetalkshh
  2. public class Person { private final long id;
 private String

    name;
 public Person(long id) { … } public Person(long id, String name) { … } public long getId() { … }
 public String getName() { … }
 public void setName(String name) { … } public boolean equals(Object o) { … } public int hashCode() { … }
 }
  3. data class Person (val id: Long, var name: String?) equals()

    hashCode() copy() toString() val = immutable / final
  4. data class Person (val id: Long, var name: String?) equals()

    hashCode() copy() toString() val = immutable / final "normale" variable
  5. data class Person (val id: Long, var name: String?) equals()

    hashCode() copy() toString() optional, d.h. null ist erlaubt "normale" variable val = immutable / final
  6. data class Person (val id: Long, var name: String?) equals()

    hashCode() copy() toString() "normale" variable optional, d.h. null ist erlaubt val = immutable / final
  7. data class Person (val id: Long, var name: String?) {

    var age: Int = 5 fun getInfos() { return "$Name, $age Jahre alt." } }
  8. data class Person (val id: Long, var name: String? =

    null) { var age: Int = 5 fun getInfos() { return "$Name, $age Jahre alt." } }
  9. data class Person (val id: Long, var name: String? =

    null) { var age: Int = 5 fun getInfos() { return "$Name, $age Jahre alt." } }
  10. data class Person (val id: Long, var name: String? =

    null) { var age: Int = 5 fun getInfos() { return "$Name, $age Jahre alt." } } keine primitiven Typen
  11. data class Person (val id: Long, var name: String? =

    null) { var age: Int = 5 fun getInfos() { return "$Name, $age Jahre alt." } }
  12. data class Person (val id: Long, var name: String? =

    null) { var age: Int = 5 fun getInfos(): String { return "$name, $age Jahre alt." } }
  13. data class Person (val id: Long, var name: String? =

    null) { var age: Int = 5 fun getInfos(): String = "$name, $age Jahre alt." }
  14. data class Person (val id: Long, var name: String? =

    null) { var age: Int = 5 fun getInfos() = "$name, $age Jahre alt." }
  15. data class Person (val id: Long, var name: String? =

    null) { var age:= 5 fun getInfos() = "$name, $age Jahre alt." }
  16. if(person != null) { println(person.name) } Smart Cast, Safe Call

    nicht nötig var person: Person? = null ...
  17. public inline fun <T, R> T.let(block: (T) -> R): R

    = block(this) Extension Funktion
  18. public inline fun <T, R> T.let(block: (T) -> R): R

    = block(this) Extension Funktion Parameter ist Funktion -> Higher order Function
  19. public inline fun <T, R> T.let(block: (T) -> R): R

    = block(this) Extension Funktion Parameter ist Funktion -> Higher order Function Zugriff auf "this"
  20. public class Util { private Util() {} public static<T,R> R

    let(T receiver, Function<T,R> block) { return block.apply(receiver); } }
  21. public inline fun <T, R> T.let(block: (T) -> R): R

    = block(this) person.let { println(it.name) }
  22. public inline fun <T, R> T.let(block: (T) -> R): R

    = block(this) person.let { println(it.name) } Util.let(person, new Function<Person, Void>() { System.out.println(person.name); }); vs
  23. val textView = TextView(context).apply { text = "Moin" ellipsize =

    TruncateAt.MARQUEE textColor = R.color.white }
  24. var TextView.drawableLeft: Drawable?
 get() = compoundDrawables[0]
 set(left) {
 setCompoundDrawablesWithIntrinsicBounds(left, compoundDrawables[1],

    compoundDrawables[2], compoundDrawables[3])
 } textView.drawableLeft = getDrawable(R.id.lightBulb) // set val left = textView.drawableLeft // get
  25. fun createHtml() = html { head { title {+"Typesafe builders

    with Kotlin"} } body { h1 {+"Typesafe builders with Kotlin"} p {+"this format can be used as an alternative markup to XML"} } }
  26. fun createHtml() = html { head { title {+"Typesafe builders

    with Kotlin"} } body { h1 {+"Typesafe builders with Kotlin"} p {+"this format can be used as an alternative markup to XML"} } } fun html(init: HTML.() -> Unit): HTML { val html = HTML() html.init() return html }
  27. fun createHtml() = html { head { title {+"Typesafe builders

    with Kotlin"} } body { h1 {+"Typesafe builders with Kotlin"} p {+"this format can be used as an alternative markup to XML"} } } fun html(init: HTML.() -> Unit): HTML { val html = HTML() html.init() return html } Extension function fun html(init: HTML.() -> Unit): HTML {
  28. fun createHtml() = html { head { title {+"Typesafe builders

    with Kotlin"} } body { h1 {+"Typesafe builders with Kotlin"} p {+"this format can be used as an alternative markup to XML"} } } fun html(init: HTML.() -> Unit): HTML { val html = HTML() html.init() return html } fun html(init: HTML.() -> Unit): HTML {
  29. fun createHtml() = html { head { title {+"Typesafe builders

    with Kotlin"} } body { h1 {+"Typesafe builders with Kotlin"} p {+"this format can be used as an alternative markup to XML"} } } fun html(init: HTML.() -> Unit): HTML { val html = HTML() html.init() return html } fun html(init: HTML.() -> Unit): HTML { Funktionen der Klasse HTML
  30. fun html(init: HTML.() -> Unit): HTML { val html =

    HTML() html.init() return html } fun html(init: HTML.() -> Unit): HTML { fun createHtml() = html { head { title {+"Typesafe builders with Kotlin"} } body { h1 {+"Typesafe builders with Kotlin"} p {+"this format can be used as an alternative markup to XML"} } }
  31. verticalLayout { padding = dip(30) editText { hint = "Name"

    textSize = 24f } editText { hint = "Password" textColor = Color.BLUE } button("Login") { onClick { ... } } }
  32. verticalLayout { padding = dip(30) editText { hint = "Name"

    textSize = 24f } editText { hint = "Password" textColor = Color.BLUE } button("Login") { onClick { ... } } } ANKO github.com/Kotlin/anko
  33. apply { plugin<AppPlugin>() plugin<KotlinAndroidPluginWrapper>() } android { buildToolsVersion("24.0.2") compileSdkVersion(24) defaultConfigExtension

    { setMinSdkVersion(21) setTargetSdkVersion(23) applicationId = "com.example.kotlingradle" versionCode = 1 versionName = "1.0-RC" } … } dependencies { … }
  34. apply { plugin<AppPlugin>() plugin<KotlinAndroidPluginWrapper>() } android { buildToolsVersion("24.0.2") compileSdkVersion(24) defaultConfigExtension

    { setMinSdkVersion(21) setTargetSdkVersion(23) applicationId = "com.example.kotlingradle" versionCode = 1 versionName = "1.0-RC" } … } dependencies { … } GRADLE gradle.org/blog/kotlin-meets-gradle/
  35. val items = listOf("CODE", "TALKS", null, "2016") items.filterNonNull() .map {

    text -> text.toLowerCase() }.reduce { a,b -> "$a.$b" }.let { println(it) } //prints code.talks.2016
  36. val items = listOf("CODE", "TALKS", null, "2016") items.filterNonNull() .map {

    text -> text.toLowerCase() }.reduce { a,b -> "$a.$b" }.let { println(it) } //prints code.talks.2016
  37. val items = listOf("CODE", "TALKS", null, "2016") items.filterNonNull() .map {

    text -> text.toLowerCase() }.reduce { a,b -> "$a.$b" }.let { println(it) } //prints code.talks.2016
  38. val items = listOf("CODE", "TALKS", null, "2016") items.filterNonNull() .map {

    text -> text.toLowerCase() }.reduce { a,b -> "$a.$b" }.let { println(it) } //prints code.talks.2016
  39. val items = listOf("CODE", "TALKS", null, "2016") items.filterNonNull() .map {

    text -> text.toLowerCase() }.reduce { a,b -> "$a.$b" }.let { println(it) } //prints code.talks.2016
  40. val items = listOf("CODE", "TALKS", null, "2016") items.filterNonNull() .map {

    text -> text.toLowerCase() }.reduce { a,b -> "$a.$b" }.let { println(it) } //prints code.talks.2016