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

Kotlin the Lighthouse Language

Dor Samet
August 08, 2017

Kotlin the Lighthouse Language

A brief introduction to Kotlin syntax as presented at the ironSource meetup on the 8th of August 2017

Dor Samet

August 08, 2017
Tweet

More Decks by Dor Samet

Other Decks in Technology

Transcript

  1. Hi There! • Software Developer for three years ◦ Medical

    Research ◦ eCommerce • Android Developer at heart and profession • Software Developer at Gett • Co-Organizer KotlinTLV
  2. So what is Kotlin? • Language developed by Jetbrains (IntelliJ)

    • Open sourced in 2011, Kotlin 1.0 on Feb 15th 2016 • 100% interoperable with Java • First class citizen language announcement in I/O 2017
  3. Do we seriously need another language? • Costs nothing to

    adopt • Concise • Negligible Runtime overhead • Solves the Nullability Problem • Awesome and Modern
  4. Concise? public class JavaPerson { private int age; private String

    name; private String address; public JavaPerson(int age, String name, String address) { this.age = age; this.name = name; this.address = address; } ...
  5. Concise..? public void setAddress(String address) { this.address = address; }

    public void setAge(int age) { this.age = age; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public String getAddress() { return address; } public String getName() { return name; }
  6. Yikes..? @Override public boolean equals(Object o) { if (this ==

    o) return true; if (o == null || getClass() != o.getClass()) return false; JavaPerson that = (JavaPerson) o; if (age != that.age) return false; if (name != null ? !name.equals(that.name) : that.name != null) return false; return address != null ? address.equals(that.address) : that.address == null; } @Override public int hashCode() { int result = age; result = 31 * result + (name != null ? name.hashCode() : 0); result = 31 * result + (address != null ? address.hashCode() : 0); return result; } }
  7. Today • Variables • Functions • Extension Functions • Null

    Safety • Intro to Object-Oriented Programming
  8. A few ground rules before we start • No static*

    • Everything that worked in Java will still work • No semicolons needed* * Except under adult supervision...
  9. val/var name = "Hanan" // Compiler will complain val name:

    String = "Yarkoni" // public final String name = “Yarkoni” val name = "Yarkoni" // Type Inference
  10. val/var var name: String = "Yarkoni" public String name =

    “Yarkoni” var name = "Yarkoni" // Type Inference name = "Hanan" // Compiler will be fine
  11. Statically Typed Language var name = "Yarkoni" name = 5

    // Compiler will complain name = "Hanan" // Compiler will be fine
  12. 100% Java Interop val sb : StringBuilder = StringBuilder("Here") //

    Java StringBuilder* sb.append(" is some").append(" text!") // Java StringBuilder methods println(sb) // Wrapper for System.out.println
  13. Functions fun maximum(first: Int, second: Int): Int { if (first

    > second) { return first } else { return second } }
  14. Functions - Default Values fun maximum(first: Int = 3, second:

    Int = 4): Int { if (first > second) { return first } else { return second } }
  15. Default Values - Java 7 public int maximum() { return

    maximum(3, 4); } public int maximum(int first) { return maximum(first, 4); } public int maximumFirstDefault(int second) { return maximum(3, second); } public int maximum(int first, int second) { return first > second ? first : second; }
  16. Function Calling // Regular call with parameters maximum(7, 6) //

    Named parameters maximum(first = 7, second = 6) maximum(second = 6, first = 7) maximum(second = 6)
  17. Functions - Concise fun maximum(first: Int = 3, second: Int

    = 4): Int { if (first > second) { return first } else { return second } }
  18. Functions - Concise fun maximum(first: Int = 3, second: Int

    = 4): Int = if (first > second) { first } else { second }
  19. Functions - Concise fun maximum(first: Int = 3, second: Int

    = 4): Int = if (first > second) first else second
  20. Functions - Concise fun maximum(first: Int = 3, second: Int

    = 4) = if (first > second) first else second
  21. Extension Functions • Adopted from C# and Gosu • “Extension

    without Inheritance” • Basically “utils” functions
  22. Extension Functions fun Int.maximum(other: Int): Int = if (this >

    other) this else other public static int maximum(int $receiver, int other) { if ($receiver > other) { return $receiver; } else { return other; } }
  23. Other Function Features • Infix - no . needed •

    Operator - plus() is + • Inline (noinline) - Inlines a function • Functions within functions - double the fun!
  24. Null Safety String amINull = "Not Null"; // Some time

    passes amINull = null; // Uh Oh // Some time passes amINull.equals("Null");
  25. • An object reference in Java may or may not

    be null • Accessing that reference at compile is ok • Accessing that reference at runtime: ◦ May be ok ◦ May crash the system Nullability Problem Defined https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare
  26. • Java 8’s solution - Optional wrapper ◦ Grammar (.ofNullable,

    .of) ◦ I have to write down Optional?? ◦ I have to ask isPresent() and then get()???? • Kotlin’s solution - Built-in Nullable type Nullability Solution
  27. Nullable Type - Chaining nullString?.toDoubleOrNull()?.compareTo(1) String nullString = ""; if

    (nullString != null) { if (Double.valueOf(nullString) != null) { return Double.valueOf(nullString).compareTo(1.0); } } return null;
  28. Object Oriented Programming - Rules • Interfaces may extend other

    interfaces • Classes my extend one other class • Classes may implement as many interfaces as needed
  29. Kotlin Rules • Enums are called enum classes • Abstract

    is a soft keyword, but abstract exists • All objects must be fully initialized at creation • Java may extend Kotlin • Kotlin may extend Java
  30. Open/Override open class CodingExample { open fun writeCode() = "I

    am writing code!" } class KotlinExample: CodingExample() { override fun writeCode() = "I am writing Kotlin Code" }
  31. Open/Override open class CodingExample { open fun writeCode() = "I

    am writing code!" } class KotlinExample: CodingExample() { override fun writeCode() = "${super.writeCode()} I am writing Kotlin Code" }
  32. Inheritance Example interface Person { fun fullName(): String } abstract

    class Bob implements Person { public abstract void fullName(); public String sayName() { return "My Name Is Bob"; } }
  33. Inheritance Example interface Person { fun fullName(): String } abstract

    class Bob(open val firstName: String): Person { abstract fun fullName() fun sayName(): String = "My Name Is Bob" }
  34. Inheritance Example abstract class Bob(open val firstName: String): Person {

    abstract fun fullName(): String //can be omitted fun sayName(): String = "My Name Is Bob" }
  35. Inheritance Example - Continued class BobDylan: Bob("Bob") { // String

    interpolation is fun override fun fullName(): String = "${sayName()} Dylan" } abstract class Bob(open val firstName: String): Person { abstract fun fullName(): String //can be omitted fun sayName(): String = "My Name Is Bob" }
  36. Inheritance Example - Continued class BobDole(override val firstName: String =

    "Bob", val lastName: String = "Dole"): Bob(firstName) { override fun fullName(): String = "I'm Bob Dole" } abstract class Bob(open val firstName: String): Person { abstract fun fullName(): String //can be omitted fun sayName(): String = "My Name Is Bob" }
  37. Data Classes data class Elvis(val firstName: String = "Elvis", val

    lastName: String = "Presley") open data class Elvis(val firstName: String = "Elvis", val lastName: String = "Presley") Compiler will complain!
  38. Sealed Classes sealed class Person(val firstName: String, val lastName: String)

    { class BobDylan: Person("Bob", "Dylan") class BobDole(val age: Int, fName: String, lName: String) : Person(fName, lName) }
  39. Smart Casting fun translatePerson(person: Person): String = when (person) {

    is Person.BobDylan -> { "${person.firstName} ${person.lastName}" } is Person.BobDole -> { "${person.fName} ${person.lName}, of age ${person.age}" } }