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.

Svetlana Isakova

November 01, 2017

Transcript

42 slides
  1. project started Timeline 2010 Feb 2016 Mar 2017 Nov 2017

    Kotlin 1.0 Kotlin 1.1 Kotlin 1.2 … …
  2. 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;
 }
 }
  3. - equals - hashCode - toString 
 data 
 class

    Person(val name: String, val age: Int)
  4. 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()
  5. 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
  6. 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, /*...*/; }
  7. 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
 }
 // ...
 }
  8. 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)
 } // ...
 }
  9. fun updateWeather(degrees: Int) {
 val (description, colour) =
 if (degrees

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


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


    degrees < 5 -> "cold" to BLUE
 degrees < 23 -> "mild" to ORANGE
 else -> "hot" to RED
 }
 }
  12. 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;
 }
  13. fun String.lastChar() = get(length - 1) this can be omitted

    Extension Functions fun String.lastChar() = this.get(this.length - 1)
  14. 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");
  15. 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?
  16. Kotlin library: extensions on collections • filter • map •

    reduce • count • find • any • flatMap • groupBy • …
  17. 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

More Decks by Svetlana Isakova

Other Decks in Programming