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

Devfest 2017

Guy Heylens
November 05, 2017

Devfest 2017

Kotlin Introduction

Guy Heylens

November 05, 2017
Tweet

More Decks by Guy Heylens

Other Decks in Technology

Transcript

  1. https://github.com/kugbel Kotlin: The Why • Started in 2010 by Jetbrains

    • A language that • Safe and Approachable • Pragmatic
  2. https://github.com/kugbel Kotlin: Timeline 0 2000 4000 6000 8000 10000 12000

    2010 2012 2013 2014 2015 2016 2017 2018 Kotlin is born Release 1.0 Gradle Spring Official Android Release 1.1 Dedicated Kotlin support for Spring 5.0
  3. https://github.com/kugbel Kotlin: The What • Statically typed language • Inspired

    by Java, Groovy, Scala, C#, amongst others • Targets • JVM / Android • Javascript • Native
  4. https://github.com/kugbel Kotlin: The What • What can we do? •

    Industrial applications • Mobile • Client side • Web • Desktop • Server side (Backend)
  5. https://github.com/kugbel Kotlin Conventions • Follows the Java Programming Language Coding

    Conventions • Types in Uppercase • Methods and Properties in lower camelCase • Semicolons are (almost) optional • Packages follow the reverse notation • be.kotlin.training.basics • Package does not have to match the folder name.
  6. https://github.com/kugbel Kotlin Tooling • JDK 6, 7, 8 and 9

    • Kotlin compiler • Editor or IDE • IntelliJ Idea, Android Studio, Eclipse, Netbeans
  7. https://github.com/kugbel fun Keyword Kotlin in .... minutes • Functions fun

    max(a: Int, b: Int): Int { return if(a > b) a else b }
  8. https://github.com/kugbel fun Keyword Parameters Kotlin in .... minutes • Functions

    fun max(a: Int, b: Int): Int { return if(a > b) a else b }
  9. https://github.com/kugbel fun Keyword Return type Parameters Kotlin in .... minutes

    • Functions fun max(a: Int, b: Int): Int { return if(a > b) a else b }
  10. https://github.com/kugbel Function body fun Keyword Return type Parameters Kotlin in

    .... minutes • Functions fun max(a: Int, b: Int): Int { return if(a > b) a else b }
  11. https://github.com/kugbel Kotlin in .... minutes • Statements and expressions Kotlin

    if is an expression as most other control structures, except for the loops. Java all control structures are statements
  12. https://github.com/kugbel Kotlin in .... minutes • Statements and expressions Kotlin

    if is an expression as most other control structures, except for the loops. Java all control structures are statements Expression vs Statement has a value can be used as a part of another expression has no value always top level element
  13. https://github.com/kugbel Kotlin in .... minutes • Functions fun max(a: Int,

    b: Int) = if (a > b) a else b No return type declaration!
  14. https://github.com/kugbel Kotlin in .... minutes • Variables var myValue: Int

    var myValue: Int = 10 val myValue: String val myValue: String = “Hello, world!”
  15. https://github.com/kugbel Kotlin in .... minutes • Variables var myValue: Int

    var myValue: Int = 10 val myValue: String val myValue: String = “Hello, world!” Initializer expression
  16. https://github.com/kugbel Kotlin in .... minutes • Variables Mutable and Immutable

    variables val => value “immutable” Can’t be reassigned
  17. https://github.com/kugbel Kotlin in .... minutes • Variables Mutable and Immutable

    variables val => value “immutable” Can’t be reassigned final in Java
  18. https://github.com/kugbel Kotlin in .... minutes • Variables Mutable and Immutable

    variables var => variable “mutable” val => value “immutable” Can’t be reassigned final in Java
  19. https://github.com/kugbel Kotlin in .... minutes • Variables Mutable and Immutable

    variables var => variable “mutable” val => value “immutable” Can’t be reassigned final in Java Can be reassigned
  20. https://github.com/kugbel Kotlin in .... minutes • Variables Mutable and Immutable

    variables var => variable “mutable” val => value “immutable” Can’t be reassigned final in Java Can be reassigned regular (non-final) Java variable
  21. https://github.com/kugbel Kotlin in .... minutes • Variables val message: String

    if (canPerformOperation()) { message = "Success" // ... perform the operation } else { message = "Failed" }
  22. https://github.com/kugbel Kotlin in .... minutes • Variables val message: String

    if (canPerformOperation()) { message = "Success" // ... perform the operation } else { message = "Failed" } val languages = arrayListOf("Java") languages.add("Kotlin")
  23. https://github.com/kugbel Kotlin in .... minutes val myValue: String = “Hello,

    world!” == val myValue = “Hello, world!” Type inference:
  24. https://github.com/kugbel Kotlin in .... minutes String templates val name :

    String = “John” val age : Int = 35 val s = “My name is $name and my age is $age”
  25. https://github.com/kugbel Kotlin in .... minutes String templates val name :

    String = “John” val age : Int = 35 val s = “My name is $name and my age is $age”
  26. https://github.com/kugbel Kotlin in .... minutes String templates val name :

    String = “John” val age : Int = 35 val s = “My name is $name and my age is $age”
  27. https://github.com/kugbel Kotlin in .... minutes val name : String =

    “John” val s = “My name is $name and my name length is {$name.length}” String templates
  28. https://github.com/kugbel Kotlin in .... minutes val s = “””This is

    a multiline string. It’s fun. Try it.””” String templates
  29. https://github.com/kugbel val s = “””This is a multiline |string. |It’s

    fun. |Try it.”””.trimMargin() Kotlin in .... minutes String templates
  30. https://github.com/kugbel val s = “””This is a multiline |string. |It’s

    fun. |Try it.”””.trimMargin() Kotlin in .... minutes .trimMargin(“>”) String templates
  31. https://github.com/kugbel Kotlin in .... minutes • Ranges and loops rangeTo

    val range = 1 .. 4 val start = 1 val end = 4 val otherRange = start .. end downTo val range = 4 downTo 1 val start = 4 val end = 1 val otherRange = start downTo end
  32. https://github.com/kugbel Kotlin in .... minutes • for loops for(a: Int

    in 1..100){ println(a) } for(a in 100 downTo 1){ println(a) } for(b in 100 downTo 1 step 5){ println(b) }
  33. https://github.com/kugbel Kotlin in .... minutes • for loops val capitals

    = listOf(“Brussels”, “Antwerp”, “Ghent”, “Hasselt”) for(capital in capitals){ println(capital) }
  34. https://github.com/kugbel Kotlin in .... minutes • while loops var i

    = 100 while(i > 0){ i -- } var x = 10 do{x -- println(x) } while (x > 6)
  35. https://github.com/kugbel Kotlin in .... minutes • break out of a

    loop for(a: Int in 1..100){ for(b: Int in 1..100){ if(b % a == 0){ //break out of the loop! } } }
  36. https://github.com/kugbel Kotlin in .... minutes • break out of a

    loop loop@ for(a: Int in 1..100){ for(b: Int in 1..100){ if(b % a == 0){ break @loop } } }
  37. https://github.com/kugbel Kotlin in .... minutes • break out of a

    loop loop@ for(a: Int in 1..100){ for(b: Int in 1..100){ if(b % a == 0){ break @loop } } } THIS IS NOT A GOTO!
  38. https://github.com/kugbel Kotlin in .... minutes • Classes Modifier Class member

    Top-level declaration public (default) Visible everywhere Visible everywhere internal Visible in a module Visible in a module protected Visible in subclasses - private Visible in a class Visible in a file
  39. https://github.com/kugbel Kotlin in .... minutes • Classes /* Java */

    public class Person { private final String name; public Person(String name) { this.name = name; } public String getName() { return name; } }
  40. https://github.com/kugbel Repetitive code Kotlin in .... minutes • Classes /*

    Java */ public class Person { private final String name; public Person(String name) { this.name = name; } public String getName() { return name; } }
  41. https://github.com/kugbel Kotlin in .... minutes • Classes class Person( val

    name: String, var isMarried: Boolean ) Read-only property: generates a field and a trivial getter
  42. https://github.com/kugbel Kotlin in .... minutes • Classes class Person( val

    name: String, var isMarried: Boolean ) Writeable property: generates a field, a getter and a setter Read-only property: generates a field and a trivial getter
  43. https://github.com/kugbel Kotlin in .... minutes • Classes //custom getters and

    setters class Person(val id: Int, var name: String, val yearOfBirth: Int){ val age: Int get() = Calendar.getInstance().get(Calendar.YEAR) - yearOfBirth var socialSecurityNumber: String = "" set(value) { if(!value.startsWith("SN")){ throw IllegalArgumentException("Social security number should start with SN") } field = value } //function in a class fun personAsString(){ println("Id: $id - Name: $name - Birthdate: $yearOfBirth ") } }
  44. https://github.com/kugbel Kotlin in .... minutes • Classes //custom getters and

    setters class Person(val id: Int, var name: String, val yearOfBirth: Int){ val age: Int get() = Calendar.getInstance().get(Calendar.YEAR) - yearOfBirth var socialSecurityNumber: String = "" set(value) { if(!value.startsWith("SN")){ throw IllegalArgumentException("Social security number should start with SN") } field = value } //function in a class fun personAsString(){ println("Id: $id - Name: $name - Birthdate: $yearOfBirth ") } }
  45. https://github.com/kugbel Kotlin in .... minutes • Classes //custom getters and

    setters class Person(val id: Int, var name: String, val yearOfBirth: Int){ val age: Int get() = Calendar.getInstance().get(Calendar.YEAR) - yearOfBirth var socialSecurityNumber: String = "" set(value) { if(!value.startsWith("SN")){ throw IllegalArgumentException("Social security number should start with SN") } field = value } //function in a class fun personAsString(){ println("Id: $id - Name: $name - Birthdate: $yearOfBirth ") } } //field (reserved word) is the backing field of the property
  46. https://github.com/kugbel Kotlin in .... minutes • Classes //custom getters and

    setters class Person(val id: Int, var name: String, val yearOfBirth: Int){ val age: Int get() = Calendar.getInstance().get(Calendar.YEAR) - yearOfBirth var socialSecurityNumber: String = "" set(value) { if(!value.startsWith("SN")){ throw IllegalArgumentException("Social security number should start with SN") } field = value } //function in a class fun personAsString(){ println("Id: $id - Name: $name - Birthdate: $yearOfBirth ") } } //field (reserved word) is the backing field of the property
  47. https://github.com/kugbel Kotlin in .... minutes • Classes data class Customer

    (var id: Int, var name: String, var email: String) public class CustomerJava { private int id; private String name; private String email; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public @NotNull String neverNull(){ return "a string"; } public String sometimesNull(){ return "a string"; } public void prettyPrint(){ System.out.printf("Id: %d - Name %s", id, name); } @Override public String toString() { return "CustomerJava{" + "id=" + id + ", name='" + name + '\'' + ", email='" + email + '\'' + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; CustomerJava that = (CustomerJava) o; if (id != that.id) return false; if (name != null ? !name.equals(that.name) : that.name != null) return false; return email != null ? email.equals(that.email) : that.email == null; } @Override public int hashCode() { int result = id; result = 31 * result + (name != null ? name.hashCode() : 0); result = 31 * result + (email != null ? email.hashCode() : 0); return result; } }
  48. https://github.com/kugbel Kotlin in .... minutes • Classes // 0 (zero)

    based enums enum class Priority{ MINOR, NORMAL, MAJOR, CRITICAL } enum class PriorityValue(val value: Int){ MINOR(-1){ override fun toString(): String { return "Minor priority" } }, NORMAL(0), MAJOR(1), CRITICAL(100); }
  49. https://github.com/kugbel Kotlin in .... minutes • Classes • First class

    citizens in Kotlin → objects first • Provide expected functionality such as properties and functions • There are no fields, only properties • We have access to properties if required in custom getters / setters • Data classes provide functionality to reduce boiler-plate code • Each enum is an instance and allows behaviour
  50. https://github.com/kugbel Kotlin in .... minutes • Interfaces • Same as

    in Java with a few twists • Allows to have default implementations (same as Java 8) • Is different from an abstract class because: • it cannot hold a state • a class can implement multiple interfaces when it cannot inherit from 
 multiple abstract classes
  51. https://github.com/kugbel Kotlin in .... minutes • Interfaces interface Interface1 {

    fun funA(){ println("Interface 1 says Hello") } } interface Interface2 { fun funA(){ println("Interface 2 says Hello") } }
  52. https://github.com/kugbel Kotlin in .... minutes • Interfaces interface Interface1 {

    fun funA(){ println("Interface 1 says Hello") } } interface Interface2 { fun funA(){ println("Interface 2 says Hello") } } class Class1and2: Interface1, Interface2{ override fun funA() { //refer with the super keyword super<Interface2>.funA() super<Interface1>.funA() } }
  53. https://github.com/kugbel Kotlin in .... minutes • Conditional flow var myString

    = "test" if(myString != " "){ println("Not empty") } else if(myString.startsWith("a")){ println("string starts with an A") } else { println("Empty") }
  54. https://github.com/kugbel Kotlin in .... minutes • Conditional flow val result

    = if(myString != " "){ true 20 // cast to Any! => in c# Object } else { false }
  55. https://github.com/kugbel Kotlin in .... minutes • Conditional flow when(result){ is

    String -> println("Excellent") is Int -> println("Number") "Value" -> println("Is a value") }
  56. https://github.com/kugbel Kotlin in .... minutes • Conditional flow var x

    = 2 when (x) { 1 -> print("x == 1") 2 -> print("x == 2") else -> { print("x is neither 1 nor 2") } }
  57. https://github.com/kugbel Kotlin in .... minutes • Conditional flow when (x)

    { 0, 1 -> print("x == 0 or 1") else -> print("neither 0 nor 1") } val validNumbers = 1..19 when (x) { in 1..10 -> print("x is in the range") in validNumbers -> print("x is valid") !in 10..20 -> print("x is outside the range") else -> print("none of the above") }
  58. https://github.com/kugbel Receiver type Kotlin in .... minutes • Extensions fun

    String.lastChar(): Char = this.get(this.length - 1)
  59. https://github.com/kugbel Extension function Receiver type Kotlin in .... minutes •

    Extensions fun String.lastChar(): Char = this.get(this.length - 1)
  60. https://github.com/kugbel Kotlin in .... minutes • Extensions fun String.capStuff(): String

    { return this.toUpperCase() } fun String.prefixStuff(prefix: String): String { return "$prefix $this" } fun main(args: Array<String>) { println(“Batman".capStuff()) // Prints out “BATMAN” println("Robin".prefixStuff("Hey").capStuff()) // Prints out “HEY ROBIN” }
  61. https://github.com/kugbel Kotlin in .... minutes • Interoperable • Navigate between

    Java and Kotlin source files. • Debug mixed language projects and step between code written in different languages. • Refactor your Java methods and have their use in Kotlin code correctly updated, and vice versa.
  62. https://github.com/kugbel Kotlin in .... minutes • Interoperable fun main(args: Array<String>)

    { val customer = CustomerJava() customer.email = "[email protected]" customer.prettyPrint() /* * Runnable is a Java interface. Through lambdas we are directly passing the println method. * */ val runnable = Runnable {println("Invoking runnable")} }
  63. https://github.com/kugbel Kotlin in .... minutes • Interoperable fun main(args: Array<String>)

    { val customer = CustomerJava() customer.email = "[email protected]" customer.prettyPrint() /* * Runnable is a Java interface. Through lambdas we are directly passing the println method. * */ val runnable = Runnable {println("Invoking runnable")} }
  64. https://github.com/kugbel • Interoperable Kotlin in .... minutes fun main(args: Array<String>)

    { val kr = KotlinRepo() val customerJava = kr.getById(10) customerJava.id } //inherit from Java class PersonKotlin: PersonJava(){ } class KotlinRepo: CustomerRepository{ override fun getById(id: Int): CustomerJava { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } override fun getAll(): MutableList<CustomerJava> { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } }
  65. https://github.com/kugbel • Interoperable Kotlin in .... minutes fun main(args: Array<String>)

    { val kr = KotlinRepo() val customerJava = kr.getById(10) customerJava.id } //inherit from Java class PersonKotlin: PersonJava(){ } class KotlinRepo: CustomerRepository{ override fun getById(id: Int): CustomerJava { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } override fun getAll(): MutableList<CustomerJava> { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } }
  66. https://github.com/kugbel Kotlin in .... minutes • Null safety class KotlinRepo:

    CustomerRepository{ override fun getById(id: Int): CustomerJava? { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } override fun getAll(): MutableList<CustomerJava>? { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } }
  67. https://github.com/kugbel Kotlin in .... minutes • Null safety class KotlinRepo:

    CustomerRepository{ override fun getById(id: Int): CustomerJava? { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } override fun getAll(): MutableList<CustomerJava>? { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } }
  68. https://github.com/kugbel Kotlin in .... minutes • Interoperable public static void

    main(String[] args) { CustomerKotlin customerKotlin = new CustomerKotlin(1, "Guy", "[email protected]"); customerKotlin.setEmail(“[email protected]”); String value = customerKotlin.someField; customerKotlin.changeStatus(Status.Current); //check the default parameter customerKotlin.changeStatus(); //Annotation set name for Java. customerKotlin.Preferential(); try { customerKotlin.loadStatistics("filename"); } catch (IOException e) { e.printStackTrace(); } //Invoke Top Level Function //Like Kotlin created a static class with a static method. //TopLevelFunctionsKt.prefix("some", "value"); UtilityClass.prefix("some", "value"); UtilityClass.getCopyrightYear(); //Talking to the extension function it requires an instance of the class "customerKotlin" CustomerKotlinKt.extensions(customerKotlin, "Test"); } data class CustomerKotlin (var id: Int, var name: String, var email: String){ @JvmField val someField = "Value" override fun toString():String{ return "{\"id\": \"$name\", \"name\": \"$name\"}" } @JvmOverloads fun changeStatus(status: Status = Status.Current){ } @JvmName("Preferential") fun makePrefered(){ } @Throws(IOException::class) fun loadStatistics(filename: String){ if(filename == ""){ throw IOException("Filename can not be empty") } } }
  69. https://github.com/kugbel Kotlin Version • Current version 1.1 • Version 1.2

    beta released 29/9/2017 • Multiplatform projects • Array literals in annotations
  70. https://github.com/kugbel Kotlin Version • Current version 1.1 • Version 1.2

    beta released 29/9/2017 • Multiplatform projects • Array literals in annotations @CacheConfig(cacheNames = arrayOf("books", "default"))
  71. https://github.com/kugbel Kotlin Version • Current version 1.1 • Version 1.2

    beta released 29/9/2017 • Multiplatform projects • Array literals in annotations @CacheConfig(cacheNames = arrayOf("books", "default")) @CacheConfig(cacheNames = ["books", "default"])
  72. https://github.com/kugbel Kotlin Version • Version 1.2 beta released 29/9/2017 •

    Multiplatform projects • Array literals in annotations • Lateinit improvements
  73. https://github.com/kugbel Kotlin Version • Version 1.2 beta released 29/9/2017 •

    Multiplatform projects • Array literals in annotations • Lateinit improvements lateinit var file: File // ... if (::file.isInitialized) { ... }
  74. https://github.com/kugbel Kotlin Version • Version 1.2 beta released 29/9/2017 •

    Multiplatform projects • Array literals in annotations • Lateinit improvements • Bound callable reference improvements
  75. https://github.com/kugbel Kotlin Version • Version 1.2 beta released 29/9/2017 •

    Multiplatform projects • Array literals in annotations • Lateinit improvements • Bound callable reference improvements this::foo
  76. https://github.com/kugbel Kotlin Version • Version 1.2 beta released 29/9/2017 •

    Multiplatform projects • Array literals in annotations • Lateinit improvements • Bound callable reference improvements this::foo ::foo
  77. https://github.com/kugbel Kotlin Version • Version 1.2 beta released 29/9/2017 •

    Multiplatform projects • Array literals in annotations • Lateinit improvements • Bound callable reference improvements • Type inference improvements
  78. https://github.com/kugbel Kotlin Version • Version 1.2 beta released 29/9/2017 •

    Multiplatform projects • Array literals in annotations • Lateinit improvements • Bound callable reference improvements • Type inference improvements val button = findViewById(R.id.button) as Button
  79. https://github.com/kugbel Kotlin Version • Version 1.2 beta released 29/9/2017 •

    Multiplatform projects • Array literals in annotations • Lateinit improvements • Bound callable reference improvements • Type inference improvements • Warning as errors
  80. https://github.com/kugbel Kotlin Version • Version 1.2 beta released 29/9/2017 •

    Multiplatform projects • Array literals in annotations • Lateinit improvements • Bound callable reference improvements • Type inference improvements • Warning as errors compileKotlin { kotlinOptions.warningsAsErrors = true }
  81. https://github.com/kugbel Kotlin Version • Version 1.2 beta released 29/9/2017 •

    Multiplatform projects • Array literals in annotations • Lateinit improvements • Bound callable reference improvements • Type inference improvements • Warning as errors • Smart cast improvements
  82. https://github.com/kugbel fun foo(x: Foo?) { val b = (x as?

    SubClass)?.subclassMethod1() if (b != null) { x.subclassMethod2() // x is smart cast to SubClass } }
  83. https://github.com/kugbel fun foo(x: Foo?) { val b = (x as?

    SubClass)?.subclassMethod1() if (b != null) { x.subclassMethod2() // x is smart cast to SubClass } } var x: String? = null if (flag) x = "Yahoo!" run { if (x != null) { println(x.length) // x is smart cast to String } }
  84. https://github.com/kugbel Kotlin Version • Version 1.2 beta released 29/9/2017 •

    Multiplatform projects • Array literals in annotations • Lateinit improvements • Bound callable reference improvements • Type inference improvements • Warning as errors • Smart cast improvements • Nested classes in Enums • kotlin.math is a new package in the Standard Library.