Kotlin? Kotlin!

Kotlin? Kotlin!

App Excellence Program 9월 발표 자료

354271902cd8ba2762d05b251dfa0f84?s=128

pluulove (노현석)

September 22, 2017
Tweet

Transcript

  1. 3.
  2. 4.
  3. 5.
  4. 6.
  5. 7.
  6. 8.
  7. 9.
  8. 19.
  9. 22.

    val text: String? = null text = 100.toString() val text:

    String? = null text = 100.toString() // Error, Immutable Data Null Safe
  10. 29.

    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" } Expression ( if / when / try ) Java Kotlin
  11. 30.

    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" } Expression ( if / when / try ) Java Kotlin
  12. 31.

    Expression ( if / when / try ) Java Kotlin

    val a = 10 val value = if (a > 10) { "Over 10" } else { "Under 10" } int a = 10; String value; if (a > 10) { value = "Over 10"; } else { value = "Under 10"; }
  13. 32.

    Expression ( if / when / try ) Java Kotlin

    val a = 10 val value = if (a > 10) "Over 10" else "Under 10" int a = 10; String value; if (a > 10) { value = "Over 10"; } else { value = "Under 10"; }
  14. 33.

    Expression ( if / when / try ) val a

    = 10 val value = if (a > 10) "Over 10" else "Under 10" Kotlin
  15. 34.

    Expression ( if / when / try ) Java Kotlin

    val value: String if (a === 1) { value = "one" } else if (a === 2) { value = "two" } else if (a === 3) { value = "three" } else { value = "another" } String value; if (a == 1) { value = "one"; } else if (a == 2) { value = "two"; } else if (a == 3) { value = "three"; } else { value = "another"; }
  16. 35.

    Expression ( if / when / try ) Java Kotlin

    val value: String when { a === 1 -> value = "one" a === 2 -> value = "two" a === 3 -> value = "three" else -> value = "another" } String value; if (a == 1) { value = "one"; } else if (a == 2) { value = "two"; } else if (a == 3) { value = "three"; } else { value = "another"; }
  17. 36.

    Expression ( if / when / try ) Java Kotlin

    val value = when(a) { 1 -> "one" 2 -> "two" 3 -> "three" else -> "another" } String value; if (a == 1) { value = "one"; } else if (a == 2) { value = "two"; } else if (a == 3) { value = "three"; } else { value = "another"; }
  18. 37.

    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") } Expression ( if / when / try ) Kotlin
  19. 38.

    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") } Expression ( if / when / try ) Kotlin Expression
  20. 39.

    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") } Expression ( if / when / try ) Kotlin Range
  21. 40.

    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") } Expression ( if / when / try ) Kotlin Not In Range
  22. 41.

    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") } Expression ( if / when / try ) Kotlin Optional, statement / expression
  23. 42.

    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
  24. 43.

    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 : false Default : 42
  25. 44.

    Default Parameter fun foo(name: String, number: Int = 42, toUpperCase:

    Boolean = false) = (if (toUpperCase) name.toUpperCase() else name) + number
  26. 45.

    Default Parameter fun foo(name: String, number: Int = 42, toUpperCase:

    Boolean = false) = (if (toUpperCase) name.toUpperCase() else name) + number
  27. 46.

    Example • setter / getter • hashCode • equals •

    toString class Item { private String type1; private int type2; private float type3; private float type4; }
  28. 47.

    Example 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;
  29. 48.

    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 + '}'; } } Field ࣻ ߸҃द? ߸҃ ೦ݾ • setter / getter • hashCode • equals • toString
  30. 49.

    Data Class data class Item( var type1: String?, var type2:

    Int, var type3: Float, var type4: Float)
  31. 50.

    Data Class data class Item( var type1: String?, var type2:

    Int, var type3: Float, var type4: Float) data Keyword
  32. 51.

    Data Class • equals() / hashCode() • toString() • componentN()

    functions • copy() // Destructuring Declarations val jane = User("Jane", 35) val (name, age) = jane println("$name, $age years of age") // prints "Jane, 35 years of age"
  33. 52.

    Singleton Class 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 Custom Method
  34. 56.

    // 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 val s = "Value" Type Inference
  35. 60.

    Smart Casts when (x) { is Int -> print(x +

    1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) }
  36. 61.

    Smart Casts when (x) { is Int -> print(x +

    1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Type Check
  37. 62.

    Smart Casts when (x) { is Int -> print(x +

    1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Smart Casts
  38. 63.

    Smart Casts when (x) { is Int -> print(x +

    1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Type Check
  39. 64.

    Smart Casts when (x) { is Int -> print(x +

    1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Smart Casts
  40. 65.

    Smart Casts when (x) { is Int -> print(x +

    1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Type Check
  41. 66.

    Smart Casts when (x) { is Int -> print(x +

    1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Smart Casts
  42. 67.

    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")
  43. 72.

    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 }
  44. 73.

    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
  45. 75.

    Higher-Order Functions and Lambdas val list = listOf(1, 2, 3)

    list.map({ x: Int -> x * 2 }) list.map({ x -> x * 2 })
  46. 76.

    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 }
  47. 77.

    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 }
  48. 78.

    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 }
  49. 80.

    ੺ݎಞ 1. Kotlin? • ࢜۽਍ ঱যۄח Ѣࠗх / ۞׬ ழ࠳

    / ࠛউх / ৈ۞ ੄ޙٜ … • Functional Programming • Hybrid Language о о૑ח ޙઁٜ • ҡଳ਷ Best Practices ࠗ੤
  50. 81.

    ੺ݎಞ 2. ೠӖ … ೠӖ … ೠӖ … • ݆੉

    হযਃ • ೠӖ۽ ػ ޙࢲ • ೠӖ۽ ػ ଼
  51. 82.
  52. 83.

    ੺ݎಞ 3. Function Programming • ೣࣻ ӝ߈ ೐۽Ӓې߁ ӝߨ •

    ೣࣻب ё୓ • FP ҙ۲ ૑ध੉ ೙ਃೣ (ex, Predicate, High-Order Function)
  53. 85.

    ੺ݎಞ 3. FP ౱ ݯߡо ୶ୌ೧઼णפ׮. ੺؀ റਗ ইש Lambda

    Clojure High-Order Function Reduce Dispatch Groovy Scala Currying Pure Function Monad Lazy
  54. 86.

    ੺ݎಞ 4. Overhead • Kotlin Compiler ী ੄೧ ࢤࢿغח ௏٘

    • ࢤпೞח Ѫࠁ׮ ؊ ݆਷ ௏٘о ࢤࢿ ms ױਤ ର੉ ೙ਃदী ٜ݅যঠೞח ௏٘ Javaࠁ׮ וܽѪ਷ ಂ౟
  55. 87.

    ੺ݎಞ 4. 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
  56. 88.

    ੺ݎಞ 5. kapt • Kotlin plugin supports annotation processors like

    Dagger or DBFlow. • kotlinc ▶︎ Kotlin Stubs ▶︎ javac ▶︎ Final Code • Clean Build ೧ঠ૑ ઁ؀۽ ز੘ೞӝب ೣ • ঌ ࣻ হח ী۞ apply plugin: 'kotlin-kapt'
  57. 89.

    ੺ݎಞ 6. ੗߄৬ ৮੹ೠ ੉߹਷ ࠛоמ • Framework Ú Java

    • Library Ú Java / Kotlin • Your Code Ú Java / Kotlin • Kotlin Compilerо Java Class Codeܳ ݅ٞ • Java to Kotlin, non-null ё୓ী null ਸ ࠁյ ҃਋ …. ী۞ ߊࢤ
  58. 90.
  59. 91.

    ൞ݎಞ 1. 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 ????
  60. 92.
  61. 93.
  62. 94.

    ൞ݎಞ 2. Kotlin Android Extensions • Kotlin Android Extensions •

    View Binding, ViewHolder (1.1.4), Parcelable (1.1.4) • Extension Function • Lazy • Anko
  63. 95.

    ൞ݎಞ 2. 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!") } } Kotlin Android Extensions <TextView android:id="@+id/textView" ... />
  64. 98.

    ൞ݎಞ 3. טযաח Kotlin • ࠶۽Ӓ / ߊ಴ / ҳӖ

    ࢠ೒ী Kotlin ࢎਊ੉ ૐо • ӝઓ Java 100% ▶ Java (N %) + Kotlin (M %) • ׮೯൤, ೠӖ۽ ੘ࢿغח Ӗٜ੉ ࢤӝҊ ੓਺ • ٘٣য, ೠӖ۽ ػ ଼ 1ӂ੉ ୹द • ழפ੄ ௏ౣܽ (୹द ৘੿)
  65. 99.

    ൞ݎಞ 4. Simple is Best • Delegation • Data Class

    • Singleton • Null Safety • Expression • Destructuring Declarations • Type Checks and Casts
  66. 100.

    val person = Person().apply { name = "Pluu" age =

    1 money = 1 phone = "123-1234-1234" // More } ൞ݎಞ 5. Code Scope
  67. 101.

    val person = Person().apply { name = "Pluu" age =

    1 money = 1 phone = "123-1234-1234" // More } ൞ݎಞ 5. Code Scope apply Calls the specified function [block] with `this` value as its receiver and returns `this` value.
  68. 103.
  69. 105.
  70. 106.
  71. 107.
  72. 108.
  73. 109.
  74. 111.

    • 2017֙ ୡࠗఠ Kotlin ҕࠗ ౥౥൤ (पઁ ҕࠗ 3~4ѐਘ) •

    GDG Kotlin झఠ٣ ଵৈ • Kotlin ੗ܐ (Kotlin Reference, Twitter, Medium, Qiita) • ࢎղ Kotlin झఠ٣ ૓೯ (ঌ۰઱Ҋ ղо ॶ۰Ҋ) ղо ೠ ೯ز
  75. 112.

    ૑Ә द੘೧ب ן૑ঋইਃ • ੉ઁ ݆਷ ࢎۈٜীѱ ҙबਸ ߉ӝ द੘

    • ই૒ ੗ܐо ੸૑݅, Բળೞѱ Ӗٜ੉ ৢۄ১ • ੗߄৬ э੉ ࢎਊ೧ب غח ੼ • ௏٘о ब೒ೞѱ ੘ࢿ оמ೧૗ • best practice ࠗ੤ • ಩և਷ ٣੗ੋ ಁఢ ੸ਊ оמ
  76. 113.

    • ן૑ ঋওযਃ • Kotlin ೙ࣻ X, খਵ۽ Kotlin਷ ழ૕

    ৘੿ • Convert Java File to Kotlin File ۽ оߺѱ द੘ • Show Kotlin Bytecode ۽ ઺р ୓௼ оמ ର੉ܳ ݅٘ח Action Item!
  77. 114.

    ଵҊ • 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)