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

1. Intro [kotlin-workshop]

1. Intro [kotlin-workshop]

Part of https://github.com/jetBrains/kotlin-workshop.

Covers:
- Introduction to Kotlin
- Java to Kotlin converter
- Extension functions

Similar talk might be found here: https://www.youtube.com/watch?v=nrzov-GpWBc.

C6997ce411091d4a51ea3caa2109c0b0?s=128

Svetlana Isakova

November 01, 2017
Tweet

Transcript

  1. Svetlana Isakova Kotlin

  2. - modern - pragmatic - Android-friendly Kotlin Programming Language 2017

    - official on Android
  3. project started Timeline 2010 Feb 2016 Mar 2017 Nov 2017

    Kotlin 1.0 Kotlin 1.1 Kotlin 1.2 … …
  4. Pragmatic - tooling - Java interop

  5. From

  6. has good tooling

  7. - completion - navigation - refactorings - inspections …

  8. can be easily mixed with Java code

  9. *.java *.class *.dex compiled to Java bytecode *.kt

  10. Kotlin code Java code You can have Java & Kotlin

    code in one project
  11. You can gradually add Kotlin to your existing app

  12. Android-friendly

  13. Android Studio is based on IntelliJ IDEA

  14. just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212

  15. No Kotlin SDK …just JDK + extensions small runtime jar

    easy Java interop
  16. Gradle & Kotlin Writing Gradle build scripts and plugins in

    Kotlin
  17. try.kotlinlang.org

  18. Kotlin Koans

  19. None
  20. J2K converter

  21. public class Person {
 private final String name;
 private final

    int age;
 
 public Person(String name, int age) {
 this.name = name;
 this.age = age;
 }
 
 public String getName() {
 return name;
 }
 
 public int getAge() {
 return age;
 }
 }
  22. - equals - hashCode - toString 
 data 
 class

    Person(val name: String, val age: Int)
  23. public class Person {
 private final String name;
 private final

    int age;
 
 public Person(String name, int age) {
 this.name = name;
 this.age = age;
 }
 
 public String getName() {
 return name;
 }
 
 public int getAge() {
 return age;
 }
 } 
 class Person( val name: String, val age: Int ) person.name person.getName()
  24. 
 class Person( val name: String, val age: Int )

    person.getName()
  25. public class Person {
 private final String name;
 private final

    int age;
 
 public Person(String name, int age) {
 this.name = name;
 this.age = age;
 }
 
 public String getName() {
 return name;
 }
 
 public int getAge() {
 return age;
 }
 } person.name
  26. public void updateWeather(int degrees) {
 String description;
 Colour colour;
 if

    (degrees < 5) {
 description = "cold";
 colour = BLUE;
 } else if (degrees < 23) {
 description = "mild";
 colour = ORANGE;
 } else {
 description = "hot";
 colour = RED;
 }
 // ...
 } enum Colour { BLUE, ORANGE, RED, /*...*/; }
  27. fun updateWeather(degrees: Int) {
 val description: String
 val colour: Colour


    if (degrees < 5) {
 description = "cold"
 colour = BLUE
 } else if (degrees < 23) {
 description = "mild"
 colour = ORANGE
 } else {
 description = "hot"
 colour = RED
 }
 // ...
 }
  28. fun updateWeather(degrees: Int) {
 val (description: String, colour: Colour) =


    if (degrees < 5) {
 Pair("cold", BLUE)
 } else if (degrees < 23) {
 Pair("mild", ORANGE)
 } else {
 Pair("hot", RED)
 } // ...
 }
  29. fun updateWeather(degrees: Int) {
 val (description, colour) =
 if (degrees

    < 5) {
 Pair("cold", BLUE)
 } else if (degrees < 23) {
 Pair("mild", ORANGE)
 } else {
 Pair("hot", RED)
 } // ...
 }
  30. fun updateWeather(degrees: Int) {
 val (description, colour) = when {


    degrees < 5 -> Pair("cold", BLUE)
 degrees < 23 -> Pair("mild", ORANGE)
 else -> Pair("hot", RED)
 } // ...
 }
  31. fun updateWeather(degrees: Int) {
 val (description, colour) = when {


    degrees < 5 -> "cold" to BLUE
 degrees < 23 -> "mild" to ORANGE
 else -> "hot" to RED
 }
 }
  32. val (description, colour) = when {
 degrees < 5 ->

    "cold" to BLUE
 degrees < 23 -> "mild" to ORANGE
 else -> "hot" to RED
 } String description;
 Colour colour; if (degrees < 5) {
 description = "cold";
 colour = BLUE;
 } else if (degrees < 23) {
 description = "mild";
 colour = ORANGE;
 } else {
 description = "hot";
 colour = RED;
 }
  33. Extension Functions

  34. fun String.lastChar() = get(length - 1) this can be omitted

    Extension Functions fun String.lastChar() = this.get(this.length - 1)
  35. import com.example.util.lastChar import com.example.util.* Extension Functions val c: Char =

    "abc".lastChar() fun String.lastChar() = get(length - 1)
  36. fun String.lastChar() = get(length - 1) Calling Extension Functions from

    Java code StringExtensions.kt char c = StringExtensionsKt.lastChar("abc"); JavaClass.java import static StringExtensionsKt.lastChar; char c = lastChar("abc");
  37. No. Because it’s a regular static method under the hood.

    fun String.lastChar() = get(length - 1) Extension Functions Is it possible to call a private member of String here?
  38. Kotlin library: extensions on collections • filter • map •

    reduce • count • find • any • flatMap • groupBy • …
  39. infix fun <A, B> A.to(that: B) = Pair(this, that) "ANSWER".to(42)

    "hot" to RED mapOf(0 to "zero", 1 to "one") The to extension function
  40. Extensions for Android Toast.makeText(this, "Thank you!”, Toast.LENGTH_SHORT).show() Activity extension function

    on Activity toast("Thank you!") this.toast("Thank you!")
  41. this.startActivity<NewActivity>("ANSWER" to 42) val intent = Intent(this, NewActivity::class.java)
 intent.putExtra("ANSWER", 42)


    startActivity(intent) Extensions for Android
  42. Copyright © 2017 https://github.com/JetBrains/kotlin-workshop