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

The Basic Kotlin

The Basic Kotlin

GDG DevFest Incheon 2017
http://devfest17in.gdg.kr

pluulove (노현석)

October 21, 2017
Tweet

More Decks by pluulove (노현석)

Other Decks in Technology

Transcript

  1. GDG DevFest
 Incheon 2017 Statically typed programming language for modern

    multiplatform applications 100% interoperable with Java™ and Android™
  2. GDG DevFest
 Incheon 2017 fun main(args: Array<String>): Unit { println("Hello,

    world!") } Basic Return Type Unit return type can be omitted
  3. GDG DevFest
 Incheon 2017 val text: String = null val

    text: String = null // Error, non-null Type Null Safe
  4. GDG DevFest
 Incheon 2017 val text: String? = null text

    = 100.toString() Null Safe val text: String? = null text = 100.toString() // Error, Immutable Data
  5. GDG DevFest
 Incheon 2017 val text: String? = null text

    = 100.toString() Null Safe Immutable
  6. GDG DevFest
 Incheon 2017 if (data != null && data.value

    != null) { data.value.toString(); } Null Safe (2)
  7. GDG DevFest
 Incheon 2017 val l: Int = if (b

    != null) b.length else -1 Null Safe (3) ~ Elvis Operator val l = b?.length ?: -1 https://kotlinlang.org/docs/reference/null-safety.html#elvis-operator
  8. GDG DevFest
 Incheon 2017 val l: Int = if (b

    != null) b.length else -1 Null Safe (3) ~ Elvis Operator val l = b?.length ?: -1 Elvis Operator https://kotlinlang.org/docs/reference/null-safety.html#elvis-operator
  9. GDG DevFest
 Incheon 2017 fun foo(node: Node): String? { val

    parent = node.getParent() ?: return null val name = node.getName() ?: 
 throw IllegalArgumentException("name expected") // ... } Null Safe (3) ~ Elvis Operator https://kotlinlang.org/docs/reference/null-safety.html#elvis-operator
  10. GDG DevFest
 Incheon 2017 Type Inference val s = "Value"

    // String val c = 'V' // Char val i = 10 // Int val l = 10L // Long val f = 10F // Float val d = 10.0 // Double val b = true // Boolean val sb = StringBuilder() // StringBuilder
  11. GDG DevFest
 Incheon 2017 Control Flow (if / when /

    try) int a = 10; String value; if (a > 10) { value = "Over 10"; } else { value = "Under 10"; } val a = 10 val value: String if (a > 10) { value = "Over 10" } else { value = "Under 10" } Java Kotlin
  12. GDG DevFest
 Incheon 2017 Control Flow (if / when /

    try) int a = 10; String value; if (a > 10) { value = "Over 10"; } else { value = "Under 10"; } val a = 10 val value = if (a > 10) { "Over 10" } else { "Under 10" } Java Kotlin
  13. GDG DevFest
 Incheon 2017 val a = 10 val value

    = if (a > 10) "Over 10" else "Under 10" Control Flow (if / when / try) Kotlin
  14. GDG DevFest
 Incheon 2017 Control Flow (if / when /

    try) String value; switch (a) { case 1: value = "one"; break; case 2: value = "two"; break; case 3: value = "three"; break; default: value = "another"; break; } val value: String when { a === 1 -> value = "one" a === 2 -> value = "two" a === 3 -> value = "three" else -> value = "another" } Java Kotlin
  15. GDG DevFest
 Incheon 2017 Control Flow (if / when /

    try) String value; switch (a) { case 1: value = "one"; break; case 2: value = "two"; break; case 3: value = "three"; break; default: value = "another"; break; } val value = when(a) { 1 -> "one" 2 -> "two" 3 -> "three" else -> "another" } Java Kotlin
  16. GDG DevFest
 Incheon 2017 when (x) { parseInt(s) -> print("s

    encodes 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") } Control Flow (if / when / try) Kotlin
  17. GDG DevFest
 Incheon 2017 when (x) { parseInt(s) -> print("s

    encodes 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") } Control Flow (if / when / try) Kotlin Expression
  18. GDG DevFest
 Incheon 2017 when (x) { parseInt(s) -> print("s

    encodes 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") } Control Flow (if / when / try) Kotlin Range
  19. GDG DevFest
 Incheon 2017 when (x) { parseInt(s) -> print("s

    encodes 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") } Control Flow (if / when / try) Kotlin Not in Range
  20. GDG DevFest
 Incheon 2017 when (x) { parseInt(s) -> print("s

    encodes 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") } Control Flow (if / when / try) Kotlin Optional, statement / expression
  21. GDG DevFest
 Incheon 2017 Default Parameter public String foo(String name,

    int number, boolean toUpperCase) { return (toUpperCase ? name.toUpperCase() : name) + number; } public String foo(String name, int number) { return foo(name, number, false); } public String foo(String name, boolean toUpperCase) { return foo(name, 42, toUpperCase); } public String foo(String name) { return foo(name, 42); } Java
  22. GDG DevFest
 Incheon 2017 Default Parameter public String foo(String name,

    int number, boolean toUpperCase) { return (toUpperCase ? name.toUpperCase() : name) + number; } public String foo(String name, int number) { return foo(name, number, false); } public String foo(String name, boolean toUpperCase) { return foo(name, 42, toUpperCase); } public String foo(String name) { return foo(name, 42); } Java Default Default
  23. GDG DevFest
 Incheon 2017 Default Parameter fun foo(name: String, number:

    Int = 42, toUpperCase: Boolean = false) = (if (toUpperCase) name.toUpperCase() else name) + number Kotlin
  24. GDG DevFest
 Incheon 2017 Default Parameter fun foo(name: String, number:

    Int = 42, toUpperCase: Boolean = false) = (if (toUpperCase) name.toUpperCase() else name) + number Kotlin
  25. GDG DevFest
 Incheon 2017 Example class Item { private String

    type1; private int type2; private float type3; private float type4; } ▸ setter / getter ▸ hashCode ▸ equals ▸ toString
  26. class Item { private String type1; private int type2; private

    float type3; private float type4; public Item(String type1, int type2, float type3, float type4) { this.type1 = type1; this.type2 = type2; this.type3 = type3; this.type4 = type4; } public String getType1() { return type1; } public void setType1(String type1) { this.type1 = type1; } GDG DevFest
 Incheon 2017 Example
  27. GDG DevFest
 Incheon 2017 class Item { private String type1;

    private int type2; private float type3; private float type4; public Item(String type1, int type2, float type3, float type4) { this.type1 = type1; this.type2 = type2; this.type3 = type3; this.type4 = type4; } public String getType1() { return type1; } public void setType1(String type1) { this.type1 = type1; } public int getType2() { return type2; } public void setType2(int type2) { this.type2 = type2; } public float getType3() { return type3; } public void setType3(float type3) { this.type3 = type3; } public float getType4() { return type4; } public void setType4(float type4) { this.type4 = type4; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Item item = (Item) o; if (type2 != item.type2) return false; if (Float.compare(item.type3, type3) != 0) return false; if (Float.compare(item.type4, type4) != 0) return false; return type1 != null ? type1.equals(item.type1) : item.type1 == null; } @Override public int hashCode() { int result = type1 != null ? type1.hashCode() : 0; result = 31 * result + type2; result = 31 * result + (type3 != +0.0f ? Float.floatToIntBits(type3) : 0); result = 31 * result + (type4 != +0.0f ? Float.floatToIntBits(type4) : 0); return result; } @Override public String toString() { return "Item{" + "type1='" + type1 + '\'' + ", type2=" + type2 + ", type3=" + type3 + ", type4=" + type4 + '}'; } } ӡ~~~~~~~~~~׮ ߸ࣻ ୶о/࢏ઁद ߸҃ ੘স਷ 
 ݅݅ೞ૑ ঋ׮ Example
  28. GDG DevFest
 Incheon 2017 Data Class data class Item( var

    type1: String?, var type2: Int, var type3: Float, var type4: Float)
  29. GDG DevFest
 Incheon 2017 Data Class data class Item( var

    type1: String?, var type2: Int, var type3: Float, var type4: Float) Data Class Keyword
  30. GDG DevFest
 Incheon 2017 Data Class data class Item(var type1:

    String?, var type2: Int, var type3: Float, var type4: Float) // Create Single variable val item = Item(1.toString(), 2, 3.toFloat(), 4.toFloat()) // Create Destructuring Declarations val (type1, type2, type3, type4) = 
 Item(1.toString(), 2, 3.toFloat(), 4.toFloat())
  31. GDG DevFest
 Incheon 2017 Singleton public class A { private

    static final A instance = new A(); private A() {} public static A getInstance() { return instance; } public void b() { System.out.println("Singleton"); } } Java // Use A.getInstance().b()
  32. GDG DevFest
 Incheon 2017 Singleton object A { fun b()

    { println("Singleton") } } Kotlin // Use A.b()
  33. GDG DevFest
 Incheon 2017 Smart Casts if (obj is String)

    { print(obj.length) } https://kotlinlang.org/docs/reference/typecasts.html#smart-casts
  34. GDG DevFest
 Incheon 2017 Smart Casts if (obj is String)

    { print(obj.length) } Type Check https://kotlinlang.org/docs/reference/typecasts.html#smart-casts
  35. GDG DevFest
 Incheon 2017 Smart Casts if (obj is String)

    { print(obj.length) } Smart Casts https://kotlinlang.org/docs/reference/typecasts.html#smart-casts
  36. GDG DevFest
 Incheon 2017 Smart Casts when (x) { is

    Int -> print(x + 1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) }
  37. GDG DevFest
 Incheon 2017 Smart Casts when (x) { is

    Int -> print(x + 1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Type Check
  38. GDG DevFest
 Incheon 2017 Smart Casts when (x) { is

    Int -> print(x + 1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Smart Casts
  39. GDG DevFest
 Incheon 2017 Smart Casts when (x) { is

    Int -> print(x + 1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Type Check
  40. GDG DevFest
 Incheon 2017 Smart Casts when (x) { is

    Int -> print(x + 1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Smart Casts
  41. GDG DevFest
 Incheon 2017 Smart Casts when (x) { is

    Int -> print(x + 1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Type Check
  42. GDG DevFest
 Incheon 2017 Smart Casts when (x) { is

    Int -> print(x + 1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Smart Casts
  43. GDG DevFest
 Incheon 2017 Extension Function inline fun Fragment.toast(message: CharSequence):

    Unit = 
 activity.toast(message) fun Context.toast(message: CharSequence) = 
 Toast.makeText(this, message, Toast.LENGTH_SHORT).show() https://github.com/Kotlin/anko/blob/d5a526512b48c5cd2e3b8f6ff14b153c2337aa22/anko/library/ static/commons/src/dialogs/Toasts.kt // Use in Activity/Fragment toast("Hello World")
  44. GDG DevFest
 Incheon 2017 fun Context.toast(message: CharSequence) = 
 Toast.makeText(this,

    message, Toast.LENGTH_SHORT).show() Extension Function Receiver Type
  45. GDG DevFest
 Incheon 2017 fun Context.toast(message: CharSequence) = 
 Toast.makeText(this,

    message, Toast.LENGTH_SHORT).show() Extension Function Function Definition
  46. GDG DevFest
 Incheon 2017 Higher-Order Functions and Lambdas fun <T,

    R> List<T>.map(transform: (T) -> R): List<R> { val result = arrayListOf<R>() for (item in this) result.add(transform(item)) return result }
  47. GDG DevFest
 Incheon 2017 Higher-Order Functions and Lambdas fun <T,

    R> List<T>.map(transform: (T) -> R): List<R> { val result = arrayListOf<R>() for (item in this) result.add(transform(item)) return result } Function Type
  48. GDG DevFest
 Incheon 2017 Higher-Order Functions and Lambdas val list

    = listOf(1, 2, 3) list.map({ x: Int -> x * 2 })
  49. GDG DevFest
 Incheon 2017 Higher-Order Functions and Lambdas val list

    = listOf(1, 2, 3) list.map({ x: Int -> x * 2 }) list.map({ x -> x * 2 })
  50. GDG DevFest
 Incheon 2017 Higher-Order Functions and Lambdas val list

    = listOf(1, 2, 3) list.map({ x: Int -> x * 2 }) list.map({ x -> x * 2 }) list.map() { x -> x * 2 }
  51. GDG DevFest
 Incheon 2017 Higher-Order Functions and Lambdas val list

    = listOf(1, 2, 3) list.map({ x: Int -> x * 2 }) list.map({ x -> x * 2 }) list.map() { x -> x * 2 } list.map { x -> x * 2 }
  52. GDG DevFest
 Incheon 2017 Higher-Order Functions and Lambdas val list

    = listOf(1, 2, 3) list.map({ x: Int -> x * 2 }) list.map({ x -> x * 2 }) list.map() { x -> x * 2 } list.map { x -> x * 2 } list.map { it * 2 }
  53. GDG DevFest
 Incheon 2017 Scope (apply, also, run, let) val

    result = StringBuilder().apply { for (letter in 'A'..'Z') { append(letter) } } apply ‣ this : StringBuilder() ‣ return : ഐ୹ೠ Receiver
  54. GDG DevFest
 Incheon 2017 val result = StringBuilder().also { for

    (letter in 'A'..'Z') { it.append(letter) } } Scope (apply, also, run, let) also ‣ it : StringBuilder() ‣ return : ഐ୹ೠ Receiver
  55. GDG DevFest
 Incheon 2017 Scope (apply, also, run, let) val

    result = StringBuilder().run { for (letter in 'A'..'Z') { append(letter) } this } run ‣ this : StringBuilder() ‣ return : Block ݃૑݄ ઴
  56. GDG DevFest
 Incheon 2017 val result = StringBuilder().let { for

    (letter in 'A'..'Z') { it.append(letter) } it } Scope (apply, also, run, let) let ‣ it : StringBuilder() ‣ return : Block ݃૑݄ ઴
  57. GDG DevFest
 Incheon 2017 Collections val numbers: MutableList<Int> = mutableListOf(1,

    2, 3) val readOnlyView: List<Int> = numbers println(numbers) // prints "[1, 2, 3]" numbers.add(4) println(readOnlyView) // prints "[1, 2, 3, 4]" readOnlyView.clear() // -> does not compile https://kotlinlang.org/docs/reference/collections.html
  58. GDG DevFest
 Incheon 2017 Collections val _items = mutableListOf<String>() val

    items: List<String> = _items.toList() // Convert Immutable val mutableItems = items.toMutableList() // Convert Mutable
  59. GDG DevFest
 Incheon 2017 Collections val items = listOf(1, 2,

    3, 4) items.first() == 1 items.last() == 4 items.filter { it % 2 == 0 } // returns [2, 4] val rwList = mutableListOf(1, 2, 3) rwList.requireNoNulls() // returns [1, 2, 3] if (rwList.none { it > 6 }) println("No items above 6”) // prints "No items above 6" val item = rwList.firstOrNull() https://kotlinlang.org/docs/reference/collections.html
  60. GDG DevFest
 Incheon 2017 Collections Iterable MutableIterable MutableCollection MutableList MutableSet

    Collection List Set ArrayList ArrayList Read-only interface Mutable interface Java classes
  61. GDG DevFest
 Incheon 2017 Type aliases data class User(val name:

    String, val job: String) typealias Pluu = User println(User("Pluu", "Developer")) // User(name=Pluu, job=Developer) println(Pluu("Pluu", "Developer")) // User(name=Pluu, job=Developer)
  62. GDG DevFest
 Incheon 2017 Type aliases typealias PluuList = List<Pluu>

    val list : PluuList = listOf( User("User Name", "Test1"), Pluu("Pluu Name", "Test2") ) println(list) // [User(name=User Name, job=Test1), User(name=Pluu Name, job=Test2)]
  63. GDG DevFest
 Incheon 2017 Type aliases typealias Predicate<T> = (T)

    -> Boolean fun foo(p: Predicate<Int>) = p(42) fun main(args: Array<String>) { val f: (Int) -> Boolean = { it > 0 } println(foo(f)) // prints "true" val p: Predicate<Int> = { it > 0 } println(listOf(1, -2).filter(p)) // prints "[1]" }
  64. GDG DevFest
 Incheon 2017 Android Studio 3.0 ▸ Tools >

    Kotlin > Configure Kotlin in Project
  65. GDG DevFest
 Incheon 2017 Project Configuration buildscript { ext.kotlin_version =

    '1.1.51' repositories { …… } dependencies { classpath ‘com.android.tools.build:gradle: 3.0.0-rc2' classpath "org.jetbrains.kotlin:kotlin- gradle-plugin:$kotlin_version" } } apply plugin: ‘kotlin-android' dependencies { compile "org.jetbrains.kotlin:kotlin- stdlib-jre7:$kotlin_version" } project build.gradle app build.gradle
  66. GDG DevFest
 Incheon 2017 Kotlin Android Extensions app build.gradle apply

    plugin: 'kotlin-android-extensions' import kotlinx.android.synthetic.main.<layout>.* // kotlinx.android.synthetic.main.activity_main.*
  67. GDG DevFest
 Incheon 2017 Kotlin Android Extensions import kotlinx.android.synthetic.main.activity_main.* class

    MyActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) textView.setText("Hello, world!") } } <TextView android:id="@+id/textView" ... /> Kotlin Android Extensions
  68. GDG DevFest
 Incheon 2017 Kotlin Android Extensions ~ Parcelable Support

    public class MyParcelable implements Parcelable { private int mData; public int describeContents() { return 0; } public void writeToParcel(Parcel out, int flags) { out.writeInt(mData); } public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() { public MyParcelable createFromParcel(Parcel in) { return new MyParcelable(in); } public MyParcelable[] newArray(int size) { return new MyParcelable[size]; } }; private MyParcelable(Parcel in) { mData = in.readInt(); } } https://github.com/Kotlin/KEEP/blob/master/proposals/extensions/android-parcelable.md
  69. GDG DevFest
 Incheon 2017 Kotlin Android Extensions ~ Parcelable Support

    build.gradle apply plugin: 'kotlin-android-extensions' androidExtensions { experimental = true }
  70. GDG DevFest
 Incheon 2017 Kotlin Android Extensions ~ Parcelable Support

    @Parcelize class MyParcelable(val data: Int): Parcelable https://github.com/Kotlin/KEEP/blob/master/proposals/extensions/android-parcelable.md
  71. GDG DevFest
 Incheon 2017 ੺ݎಞ ‣ ۞׬ழ࠳ ‣ Functional Programming

    ‣ Hybrid Language о о૑ח ޙઁٜ ‣ ೠӖ ೠӖ ೠӖ
  72. GDG DevFest
 Incheon 2017 ੺ݎಞ ~ Overhead ▸ Kotlin Compiler

    ী ੄೧ ࢤࢿغח ௏٘ ▸ ࢤпೞח Ѫࠁ׮ ؊ ݆਷ ௏٘о ࢤࢿ ▸ ೞ૑݅, ೙ਃदী ٜ݅যঠೞח ௏٘ ▸ ms ױਤ ର੉ ▸ Javaࠁ׮ וܽѪ਷ ಂ౟
  73. GDG DevFest
 Incheon 2017 ੺ݎಞ ~ Overhead fun test(x: Any)

    { if (x is String) { println(x.length) println(x.length) println(x.length) println(x.length) } } public static final void test(@NotNull Object x) { Intrinsics.checkParameterIsNotNull(x, "x"); if (x instanceof String) { int var1 = ((String)x).length(); System.out.println(var1); var1 = ((String)x).length(); System.out.println(var1); var1 = ((String)x).length(); System.out.println(var1); var1 = ((String)x).length(); System.out.println(var1); } } Kotlin Decompile
  74. GDG DevFest
 Incheon 2017 ੺ݎಞ ~ kapt ▸ Kotlin plugin

    supports annotation processors like Dagger or DBFlow. ▸ kotlinc ▶︎ Kotlin Stubs ▶︎ javac ▶︎ Final Code ▸ Clean Build ೧ঠ૑ ઁ؀۽ ز੘ೞӝب ೣ ▸ ঌ ࣻ হח ী۞
  75. GDG DevFest
 Incheon 2017 ੺ݎಞ ~ ੗߄৬੄ ੉߹਷ ࠛоמ ▸

    Framework Ú Java ▸ Library Ú Java / Kotlin ▸ Your Code Ú Java / Kotlin ▸ Kotlin Compilerо Java Class Codeܳ ݅ٞ ▸ Java to Kotlin, non-null ё୓ী null ਸ ࠁյ ҃਋ …. ী۞ ߊࢤ
  76. GDG DevFest
 Incheon 2017 ൞ݎಞ ‣ Java ޙઁ ೧Ѿ ‣

    Kotlin Android Extensions ‣ טযաח Kotlin ‣ Simple is Best ‣ Support Jetbrains
  77. GDG DevFest
 Incheon 2017 ൞ݎಞ ~ Java ޙઁ ೧Ѿ Java

    6 2006 Java 7 2011 Java 8 2014 Android 1.0 2008 Java 7 Support 2013 Java 8 Support Jack / desugar Different for every version Java 9 ????
  78. GDG DevFest
 Incheon 2017 ൞ݎಞ ~ טযաח Kotlin ▸ ࠶۽Ӓ

    / ߊ಴ / ҳӖ ࢠ೒ী Kotlin ࢎਊ੉ ૐо ▸ ӝઓ Java 100% ▶︎ Java (N %) + Kotlin (M %) ▸ ׮೯൤, ೠӖ۽ ੘ࢿغח Ӗٜ੉ ࢤӝҊ ੓਺ ▸ ٘٣য, ೠӖ۽ ػ ଼ 1ӂ੉ ୹द ▸ ழפ੄ ௏ౣܽ (୹द ৘੿)
  79. GDG DevFest
 Incheon 2017 Simple is Beset ▸ Delegation ▸

    Data Class ▸ Singleton ▸ Null Safety ▸ Expression ▸ Destructuring Declarations ▸ Type Checks and Casts
  80. GDG DevFest
 Incheon 2017 ൞ݎಞ ~ Support Jetbrains ▸ Show

    Kotlin Bytecode ▶︎ Decompile ▸ Decompile ػ Java ௏٘۽ ୓௼ оמ
  81. GDG DevFest
 Incheon 2017 Realm Report Q4 2017 Java 85.3%

    Kotlin 14.7% 2017/09/25 Java 49.3% Kotlin 50.7% 2018/12/10
  82. GDG DevFest
 Incheon 2017 ଵҊ ▸ Kotlin Reference (https://kotlinlang.org/docs/reference/) ▸

    Get Started with Kotlin — It is here to stay (https://medium.com/peachstudio/get-started- with-kotlin-it-is-here-to-stay-1eaac85ae6a0) ▸ Kotlin: A New Hope in a Java 6 Wasteland (https://academy.realm.io/posts/droidcon- michael-pardo-kotlin/) ▸ Kotlin੄ ࡄҗ Ӓܿ੗ (http://tech.lezhin.com/2017/08/03/the-case-against-kotlin) ▸ Beyond Kotlin - Advanced features for API Makers (https://speakerdeck.com/agiuliani/ beyond-kotlin-advanced-features-for-api-makers) ▸ Develop your next app with Kotlin - AndroidRennes 2017 (https://speakerdeck.com/agiuliani/ develop-your-next-app-with-kotlin-androidrennes-2017)