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

Introduction to Kotlin, Pt. 1

Introduction to Kotlin, Pt. 1

Internal talk given to the Android team at BAM Tech.

Richard Cirerol

September 01, 2016
Tweet

More Decks by Richard Cirerol

Other Decks in Programming

Transcript

  1. Why Kotlin? • Concise • Expressive • Java-interoperable • Easy

    to integrate Introduction to Kotlin Pt. 1, Richard Cirerol
  2. Is Java 6 that bad?1 1 "Advancing Android Development with

    Kotlin", Jake Wharton Introduction to Kotlin Pt. 1, Richard Cirerol
  3. Problem: Date and Calendar APIs1 • Use JodaTime or ThreeTenBP

    1 "Advancing Android Development with Kotlin", Jake Wharton Introduction to Kotlin Pt. 1, Richard Cirerol
  4. Problem: Lack of Java 8 Streams1 List<String> list = people.stream()

    .map(Person::getName).collect(Collectors.toList()); 1 "Advancing Android Development with Kotlin", Jake Wharton Introduction to Kotlin Pt. 1, Richard Cirerol
  5. Problem: Lack of Java 8 Streams1 • Use Guava Collections

    • Use Apache Collections • Use RxJava (with Retrolambda) List<String> list = Observable.from(people) .map(Person::getName).toBlocking().toList() 1 "Advancing Android Development with Kotlin", Jake Wharton Introduction to Kotlin Pt. 1, Richard Cirerol
  6. Problem: Lack of Lambdas1 • Use Retrolambda 1 "Advancing Android

    Development with Kotlin", Jake Wharton Introduction to Kotlin Pt. 1, Richard Cirerol
  7. Problem: Lack of extension methods1 • Use utility classes 1

    "Advancing Android Development with Kotlin", Jake Wharton Introduction to Kotlin Pt. 1, Richard Cirerol
  8. Problem: NullPointerException1 The "billion-dollar" mistake • Null guard wrappers 1

    "Advancing Android Development with Kotlin", Jake Wharton Introduction to Kotlin Pt. 1, Richard Cirerol
  9. Problem: Java has the unique ability to make even the

    easiest possible statement so very tedious and VERBOSE.1 • For anonymous classes, use Retrolambda instead • Otherwise, you're SOL 1 "Advancing Android Development with Kotlin", Jake Wharton Introduction to Kotlin Pt. 1, Richard Cirerol
  10. Why Kotlin? Statically typed programming language for the JVM, Android

    and the browser 100% interoperable with Java™ -- JetBrains Introduction to Kotlin Pt. 1, Richard Cirerol
  11. Why Kotlin? Think of Kotlin as C# for the JVM,

    Scala the Good Parts, Java++ or simply a decent general purpose language that won't require the blood of your first born. -- James Hughes (https://yobriefca.se/blog/2016/02/24/ kotlin-2-years-on/) Introduction to Kotlin Pt. 1, Richard Cirerol
  12. Other JVM options: • Scala • Xtend • Clojure •

    Ceylon • Groovy Introduction to Kotlin Pt. 1, Richard Cirerol
  13. Isn't Kotlin really new? Yes and no. • First commit

    was in November 2010 • v1.0 RTW in February 2016 • 1.0.3 is current stable • 1.0.4 is in EAP • Additional updates for performance, tooling, bug fixes Introduction to Kotlin Pt. 1, Richard Cirerol
  14. Isn't Kotlin really new? ...and yes. • v1.1 is in

    development • Backwards-compatible • Focus on using Java 8 features Introduction to Kotlin Pt. 1, Richard Cirerol
  15. What are the chances that it will go away soon?

    Not likely. • JetBrains is building its products on Kotlin • JetBrains is not building Kotlin as a revenue source • Kotlin is open-source (125+ contributors, including 20+ from JetBrains) Introduction to Kotlin Pt. 1, Richard Cirerol
  16. Isn't it hard to learn? • Has C roots •

    Similar to Java and C# • Very similar to Swift • In no way similar to Objective-C ! Introduction to Kotlin Pt. 1, Richard Cirerol
  17. Java 6 Problem: Date and Calendar APIs • Same solution

    - use JodaTime or ThreeTenBP • v1.1 will likely expose java.time Introduction to Kotlin Pt. 1, Richard Cirerol
  18. Java 6 Problem: Lack of Java 8 Streams • Part

    of the standard library // Java 8 stream/collect List<String> list = people.stream() .map(Person::getName).collect(Collectors.toList()); // Kotlin val list = people.map { it.name } Introduction to Kotlin Pt. 1, Richard Cirerol
  19. Java 6 Problem: Lack of Lambdas • Part of the

    standard library // Java 8 stream/collect List<String> list = people.stream() .map(Person::getName).collect(Collectors.toList()); // Kotlin val list = people.map { it.name } //OR val list = people.map { person -> person.name } Introduction to Kotlin Pt. 1, Richard Cirerol
  20. Java 6 Problem: Lack of extension methods • Part of

    the standard library fun String.ellipsize(size: Int): String { return "${this.substring(0, size)}..." } "Hello, Kotlin".ellipsize(5) // Hello... Introduction to Kotlin Pt. 1, Richard Cirerol
  21. Java 6 Problem: NPE // compiler exception val name: String

    = null // Must make type nullable val name: String? = null // compiler exception, name can be null now, cannot call length // length type is inferred val length = name.length // Inline check for null, name?.length evaluates to `null` if name==null // Provide alternative using Elvis operator val length = name?.length ?: 0 // Can force compiler to assume not null using double-bang // Possible NPE at runtime (but this is your own fault) val length = name!!.length Introduction to Kotlin Pt. 1, Richard Cirerol
  22. Java 6 Problem: Verbosity public class Person { private String

    firstName; private String lastName; private String email; public Person(final String firstName, final String lastName, final String email) { this.firstName = firstName; this.lastName = lastName; this.email = email; } public String getFirstName() { return firstName; } public void setFirstName(final String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(final String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(final String email) { this.email = email; } @Override public boolean equals(final Object o) { if (this == o) return true; if (!(o instanceof Person)) return false; final Person person = (Person) o; if (firstName != null ? !firstName.equals(person.firstName) : person.firstName != null) return false; if (lastName != null ? !lastName.equals(person.lastName) : person.lastName != null) return false; return email != null ? email.equals(person.email) : person.email == null; } @Override public int hashCode() { int result = firstName != null ? firstName.hashCode() : 0; result = 31 * result + (lastName != null ? lastName.hashCode() : 0); result = 31 * result + (email != null ? email.hashCode() : 0); return result; } @Override public String toString() { return "Person{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + ", email='" + email + '\'' + '}'; } } Introduction to Kotlin Pt. 1, Richard Cirerol
  23. Java 6 Problem: Verbosity class Customer(val firstName: String, val lastName:

    String, val email: String) // no hashcode(), equals(), or toString() Introduction to Kotlin Pt. 1, Richard Cirerol
  24. Java 6 Problem: Verbosity data class Customer(val firstName: String, val

    lastName: String, val email: String) // has hashcode(), equals(), and toString() // v1.0 cannot subclass, v1.1 can subclass Introduction to Kotlin Pt. 1, Richard Cirerol
  25. The Language: Classes //java public class Foo { } //kotlin

    class Foo Introduction to Kotlin Pt. 1, Richard Cirerol
  26. The Language: Classes //java public final class Foo { private

    Bar bar; public Foo(@NotNull Bar bar){ this.bar = bar; } } //kotlin class Foo(var bar: Bar) Introduction to Kotlin Pt. 1, Richard Cirerol
  27. The Language: Classes //java public final class Foo { private

    final Bar bar; public Foo(@NotNull Bar bar){ this.bar = bar; } } //kotlin class Foo(val bar: Bar) Introduction to Kotlin Pt. 1, Richard Cirerol
  28. The Language: Classes //java public final class Foo { private

    final Bar bar; private final String baz; public Foo(@NotNull Bar bar){ this(bar, "baz") } public Foo(@NotNull Bar bar, @NotNull String baz){ this.bar = bar; this.baz = baz; } } //kotlin class Foo(val bar:Bar, val baz:String = "baz") Introduction to Kotlin Pt. 1, Richard Cirerol
  29. The Language: Classes //java public final class Foo { private

    final Bar bar; private final String baz; public Foo(@NotNull Bar bar){ this(bar, "baz") } public Foo(@NotNull Bar bar, @NotNull String baz){ this.bar = bar; this.baz = baz; } } //kotlin @JvmOverloads class Foo(val bar:Bar, val baz:String = "baz") Introduction to Kotlin Pt. 1, Richard Cirerol
  30. The Language: Methods //java public void doSomething() { // do

    something } //kotlin fun doSomething(): Unit { // do something } Introduction to Kotlin Pt. 1, Richard Cirerol
  31. The Language: Methods //java public void doSomething() { // do

    something } //kotlin fun doSomething() { // do something } Introduction to Kotlin Pt. 1, Richard Cirerol
  32. The Language: Methods //java void doSomething() { // do something

    } //kotlin internal fun doSomething() { // do something } Introduction to Kotlin Pt. 1, Richard Cirerol
  33. The Language: Methods //java private void doSomething() { // do

    something } //kotlin private fun doSomething() { // do something } Introduction to Kotlin Pt. 1, Richard Cirerol
  34. The Language: Methods //java public int getNumber() { return 0;

    } //kotlin fun getNumber(): Int { return 0 } Introduction to Kotlin Pt. 1, Richard Cirerol
  35. The Language: Methods //java public int getNumber() { return 0;

    } //kotlin fun getNumber() = 0 Introduction to Kotlin Pt. 1, Richard Cirerol
  36. The Language: Anonymous Methods Click-listener Example2 //java public interface OnClickListener

    { void onClick(View v); } view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { doSomething("Click"); } }); 2 http://antonioleiva.com/functional-programming-android-kotlin-lambdas/ Introduction to Kotlin Pt. 1, Richard Cirerol
  37. The Language: Anonymous Methods Click-listener Example2 //kotlin view.setOnClickListener(object : OnClickListener

    { override fun onClick(v: View) { doSomething("Click") } }) 2 http://antonioleiva.com/functional-programming-android-kotlin-lambdas/ Introduction to Kotlin Pt. 1, Richard Cirerol
  38. The Language: Anonymous Methods Click-listener Example2 //kotlin view.setOnClickListener({ view ->

    doSomething("Click") }) view.setOnClickListener({ doSomething("Click") }) view.setOnClickListener(){ doSomething("Click") } view.setOnClickListener { doSomething("Click")} 2 http://antonioleiva.com/functional-programming-android-kotlin-lambdas/ Introduction to Kotlin Pt. 1, Richard Cirerol