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

Android Entwicklung mit Kotlin

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

Android Entwicklung mit Kotlin

Avatar for Volkmar Vogel

Volkmar Vogel

January 26, 2017

Other Decks in Programming

Transcript

  1. • Entwickler: JetBrains • Version 1.0 am 15 Februar 2016

    • Statisch typisierte Programmiersprache • Kompiliert zu Java 6 Bytecode
  2. public class MyClass {
 
 private final String text;
 


    public MyClass(String text) {
 this.text = text;
 }
 
 public String getText() {
 return text;
 }
 }
  3. public class DataClass {
 
 private final String nonNull;
 private

    final String nullable;
 
 public DataClass(@NonNull String nonNull,
 @Nullable String nullable) {
 
 this.nonNull = nonNull;
 this.nullable = nullable;
 }
 }
  4. I call it my billion-dollar mistake. It was the invention

    of the null reference in 1965. […] This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years. Sir Tony Hoare (Quelle: Wikipedia)
  5. var nonNull: String = "Hallo World"
 nonNull = null //

    kompiliert nicht
 
 var nullable: String? = "Hallo World"
 nullable = null
  6. data class Person(val name: String?,
 val isMale: Boolean)
 
 val

    name = if (person.name == null) "Volkmar" else person.name
  7. ?:)

  8. data class Person(val name: String?,
 val isMale: Boolean)
 
 var

    personList: List<Person?>? = null
 personList
 ?.filterNotNull()
  9. data class Person(val name: String?,
 val isMale: Boolean)
 
 var

    personList: List<Person?>? = null
 personList
 ?.filterNotNull()
 ?.filter { it.name != null }
  10. data class Person(val name: String?,
 val isMale: Boolean)
 
 var

    personList: List<Person?>? = null
 personList
 ?.filterNotNull()
 ?.filter { it.name != null }
 ?.map {
 "${if (it.isMale) "Mr."
 else "Mrs."} ${it.name}"
 }
  11. data class Person(val name: String?,
 val isMale: Boolean)
 
 var

    personList: List<Person?>? = null
 personList
 ?.filterNotNull()
 ?.filter { it.name != null }
 ?.map {
 "${if (it.isMale) "Mr."
 else "Mrs."} ${it.name}"
 }
 ?.forEach(::println)
  12. public class ContextUtils {
 public static void toast(Context ctx, String

    s) {
 Toast.makeText(ctx, s, Toast.LENGTH_SHORT).show();
 }
 }
  13. val func1: () -> Unit = { /* do stuff

    */ }
 
 val func2: (Int) -> String = Int::toString
 
 val func3: (View) -> Unit = { it.visibility = View.GONE }
 view.setOnClickListener(func3)
  14. //Collections.kt public inline fun <T> Iterable<T>.forEach(action: (T) -> Unit): Unit

    {
 for (element in this) action(element)
 } public inline fun <T, R> Iterable<T>.map(transform: (T) -> R): List<R> {
 return mapTo(ArrayList<R>(collectionSizeOrDefault(10)), transform)
 }