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

Kotlin? Kotlin!

Kotlin? Kotlin!

App Excellence Program 9월 발표 자료

pluulove (노현석)

September 22, 2017
Tweet

More Decks by pluulove (노현석)

Other Decks in Technology

Transcript

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

    String? = null text = 100.toString() // Error, Immutable Data Null Safe
  2. 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
  3. 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
  4. 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"; }
  5. 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"; }
  6. Expression ( if / when / try ) val a

    = 10 val value = if (a > 10) "Over 10" else "Under 10" Kotlin
  7. 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"; }
  8. 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"; }
  9. 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"; }
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. Default Parameter fun foo(name: String, number: Int = 42, toUpperCase:

    Boolean = false) = (if (toUpperCase) name.toUpperCase() else name) + number
  18. Default Parameter fun foo(name: String, number: Int = 42, toUpperCase:

    Boolean = false) = (if (toUpperCase) name.toUpperCase() else name) + number
  19. Example • setter / getter • hashCode • equals •

    toString class Item { private String type1; private int type2; private float type3; private float type4; }
  20. 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;
  21. 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
  22. Data Class data class Item( var type1: String?, var type2:

    Int, var type3: Float, var type4: Float)
  23. Data Class data class Item( var type1: String?, var type2:

    Int, var type3: Float, var type4: Float) data Keyword
  24. 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"
  25. 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
  26. // 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
  27. Smart Casts when (x) { is Int -> print(x +

    1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) }
  28. Smart Casts when (x) { is Int -> print(x +

    1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Type Check
  29. Smart Casts when (x) { is Int -> print(x +

    1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Smart Casts
  30. Smart Casts when (x) { is Int -> print(x +

    1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Type Check
  31. Smart Casts when (x) { is Int -> print(x +

    1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Smart Casts
  32. Smart Casts when (x) { is Int -> print(x +

    1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Type Check
  33. Smart Casts when (x) { is Int -> print(x +

    1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Smart Casts
  34. 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")
  35. 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 }
  36. 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
  37. Higher-Order Functions and Lambdas val list = listOf(1, 2, 3)

    list.map({ x: Int -> x * 2 }) list.map({ x -> x * 2 })
  38. 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 }
  39. 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 }
  40. 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 }
  41. ੺ݎಞ 1. Kotlin? • ࢜۽਍ ঱যۄח Ѣࠗх / ۞׬ ழ࠳

    / ࠛউх / ৈ۞ ੄ޙٜ … • Functional Programming • Hybrid Language о о૑ח ޙઁٜ • ҡଳ਷ Best Practices ࠗ੤
  42. ੺ݎಞ 2. ೠӖ … ೠӖ … ೠӖ … • ݆੉

    হযਃ • ೠӖ۽ ػ ޙࢲ • ೠӖ۽ ػ ଼
  43. ੺ݎಞ 3. Function Programming • ೣࣻ ӝ߈ ೐۽Ӓې߁ ӝߨ •

    ೣࣻب ё୓ • FP ҙ۲ ૑ध੉ ೙ਃೣ (ex, Predicate, High-Order Function)
  44. ੺ݎಞ 3. FP ౱ ݯߡо ୶ୌ೧઼णפ׮. ੺؀ റਗ ইש Lambda

    Clojure High-Order Function Reduce Dispatch Groovy Scala Currying Pure Function Monad Lazy
  45. ੺ݎಞ 4. Overhead • Kotlin Compiler ী ੄೧ ࢤࢿغח ௏٘

    • ࢤпೞח Ѫࠁ׮ ؊ ݆਷ ௏٘о ࢤࢿ ms ױਤ ର੉ ೙ਃदী ٜ݅যঠೞח ௏٘ Javaࠁ׮ וܽѪ਷ ಂ౟
  46. ੺ݎಞ 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
  47. ੺ݎಞ 5. kapt • Kotlin plugin supports annotation processors like

    Dagger or DBFlow. • kotlinc ▶︎ Kotlin Stubs ▶︎ javac ▶︎ Final Code • Clean Build ೧ঠ૑ ઁ؀۽ ز੘ೞӝب ೣ • ঌ ࣻ হח ী۞ apply plugin: 'kotlin-kapt'
  48. ੺ݎಞ 6. ੗߄৬ ৮੹ೠ ੉߹਷ ࠛоמ • Framework Ú Java

    • Library Ú Java / Kotlin • Your Code Ú Java / Kotlin • Kotlin Compilerо Java Class Codeܳ ݅ٞ • Java to Kotlin, non-null ё୓ী null ਸ ࠁյ ҃਋ …. ী۞ ߊࢤ
  49. ൞ݎಞ 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 ????
  50. ൞ݎಞ 2. Kotlin Android Extensions • Kotlin Android Extensions •

    View Binding, ViewHolder (1.1.4), Parcelable (1.1.4) • Extension Function • Lazy • Anko
  51. ൞ݎಞ 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" ... />
  52. ൞ݎಞ 3. טযաח Kotlin • ࠶۽Ӓ / ߊ಴ / ҳӖ

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

    • Singleton • Null Safety • Expression • Destructuring Declarations • Type Checks and Casts
  54. val person = Person().apply { name = "Pluu" age =

    1 money = 1 phone = "123-1234-1234" // More } ൞ݎಞ 5. Code Scope
  55. 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.
  56. • 2017֙ ୡࠗఠ Kotlin ҕࠗ ౥౥൤ (पઁ ҕࠗ 3~4ѐਘ) •

    GDG Kotlin झఠ٣ ଵৈ • Kotlin ੗ܐ (Kotlin Reference, Twitter, Medium, Qiita) • ࢎղ Kotlin झఠ٣ ૓೯ (ঌ۰઱Ҋ ղо ॶ۰Ҋ) ղо ೠ ೯ز
  57. ૑Ә द੘೧ب ן૑ঋইਃ • ੉ઁ ݆਷ ࢎۈٜীѱ ҙबਸ ߉ӝ द੘

    • ই૒ ੗ܐо ੸૑݅, Բળೞѱ Ӗٜ੉ ৢۄ১ • ੗߄৬ э੉ ࢎਊ೧ب غח ੼ • ௏٘о ब೒ೞѱ ੘ࢿ оמ೧૗ • best practice ࠗ੤ • ಩և਷ ٣੗ੋ ಁఢ ੸ਊ оמ
  58. • ן૑ ঋওযਃ • Kotlin ೙ࣻ X, খਵ۽ Kotlin਷ ழ૕

    ৘੿ • Convert Java File to Kotlin File ۽ оߺѱ द੘ • Show Kotlin Bytecode ۽ ઺р ୓௼ оמ ର੉ܳ ݅٘ח Action Item!
  59. ଵҊ • 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)