Share
Embed
Transcript
115 slides
-
Basic fun main(args: Array<String>): Unit { println("Hello, world!") } Unit
return type can be omitted -
Null Safe val text: String = null val text: String
= null // Error, non-null Type -
val text: String? = null text = 100.toString() val text:
String? = null text = 100.toString() // Error, Immutable Data Null Safe -
Null Safe if (data != null && data.value != null)
{ data.value.toString() } -
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 -
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 -
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"; } -
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"; } -
Expression ( if / when / try ) val a
= 10 val value = if (a > 10) "Over 10" else "Under 10" Kotlin -
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"; } -
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"; } -
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"; } -
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 -
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 -
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 -
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 -
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 -
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 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 -
Default Parameter fun foo(name: String, number: Int = 42, toUpperCase:
Boolean = false) = (if (toUpperCase) name.toUpperCase() else name) + number -
Default Parameter fun foo(name: String, number: Int = 42, toUpperCase:
Boolean = false) = (if (toUpperCase) name.toUpperCase() else name) + number -
Example • setter / getter • hashCode • equals •
toString class Item { private String type1; private int type2; private float type3; private float type4; } -
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; -
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 -
Data Class data class Item( var type1: String?, var type2:
Int, var type3: Float, var type4: Float) -
Data Class data class Item( var type1: String?, var type2:
Int, var type3: Float, var type4: Float) data Keyword -
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" -
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 -
Object Class object A { fun b() { println("Singleton") }
} // Use A.b() -
Object Class object A { fun b() { println("Singleton") }
} Object declarations -
// 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 -
Smart Casts when (x) { is Int -> print(x +
1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } -
Smart Casts when (x) { is Int -> print(x +
1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Type Check -
Smart Casts when (x) { is Int -> print(x +
1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Smart Casts -
Smart Casts when (x) { is Int -> print(x +
1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Type Check -
Smart Casts when (x) { is Int -> print(x +
1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Smart Casts -
Smart Casts when (x) { is Int -> print(x +
1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Type Check -
Smart Casts when (x) { is Int -> print(x +
1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Smart Casts -
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") -
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 } -
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 -
Higher-Order Functions and Lambdas val list = listOf(1, 2, 3)
list.map({ x: Int -> x * 2 }) -
Higher-Order Functions and Lambdas val list = listOf(1, 2, 3)
list.map({ x: Int -> x * 2 }) list.map({ x -> x * 2 }) -
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 } -
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 } -
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 } -
ݎಞ 1. Kotlin? • ࢜۽ যۄח Ѣࠗх / ۞ ழ࠳
/ ࠛউх / ৈ۞ ޙٜ … • Functional Programming • Hybrid Language о оח ޙઁٜ • ҡଳ Best Practices ࠗ -
ݎಞ 2. ೠӖ … ೠӖ … ೠӖ … • ݆
হযਃ • ೠӖ۽ ػ ޙࢲ • ೠӖ۽ ػ ଼ -
ݎಞ 3. Function Programming • ೣࣻ ӝ߈ ۽Ӓې߁ ӝߨ •
ೣࣻب ё • FP ҙ۲ ध ਃೣ (ex, Predicate, High-Order Function) -
ݎಞ 3. FP ݯߡо ୶ୌ೧઼णפ. റਗ ইש Lambda
Clojure High-Order Function Reduce Dispatch Groovy Scala Currying Pure Function Monad Lazy -
ݎಞ 4. Overhead • Kotlin Compiler ী ೧ ࢤࢿغח ٘
• ࢤпೞח Ѫࠁ ؊ ݆ ٘о ࢤࢿ ms ױਤ ର ਃदী ٜ݅যঠೞח ٘ Javaࠁ וܽѪ ಂ -
ݎಞ 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 -
ݎಞ 5. kapt • Kotlin plugin supports annotation processors like
Dagger or DBFlow. • kotlinc ▶︎ Kotlin Stubs ▶︎ javac ▶︎ Final Code • Clean Build ೧ঠ ઁ۽ زೞӝب ೣ • ঌ ࣻ হח ী۞ apply plugin: 'kotlin-kapt' -
ݎಞ 6. ߄৬ ৮ೠ ߹ ࠛоמ • Framework Ú Java
• Library Ú Java / Kotlin • Your Code Ú Java / Kotlin • Kotlin Compilerо Java Class Codeܳ ݅ٞ • Java to Kotlin, non-null ёী null ਸ ࠁյ ҃ …. ী۞ ߊࢤ -
൞ݎಞ 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 ???? -
൞ݎಞ 2. Kotlin Android Extensions • Kotlin Android Extensions •
View Binding, ViewHolder (1.1.4), Parcelable (1.1.4) • Extension Function • Lazy • Anko -
൞ݎಞ 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" ... /> -
൞ݎಞ 2. Kotlin Android Extensions apply plugin: 'kotlin-android-extensions' import kotlinx.android.synthetic.main.<layout>.*
// kotlinx.android.synthetic.main.activity_main.* -
൞ݎಞ 3. טযաח Kotlin GitHub ഛੋ (2017/09/20 ӝળ) • Stars
: 5ਤ • Language : 30ਤ -
൞ݎಞ 3. טযաח Kotlin • ࠶۽Ӓ / ߊ / ҳӖ
ࢠী Kotlin ࢎਊ ૐо • ӝઓ Java 100% ▶ Java (N %) + Kotlin (M %) • ೯, ೠӖ۽ ࢿغח Ӗٜ ࢤӝҊ • ٘٣য, ೠӖ۽ ػ ଼ 1ӂ द • ழפ ౣܽ (द ) -
൞ݎಞ 4. Simple is Best • Delegation • Data Class
• Singleton • Null Safety • Expression • Destructuring Declarations • Type Checks and Casts -
val person = Person().apply { name = "Pluu" age =
1 money = 1 phone = "123-1234-1234" // More } ൞ݎಞ 5. Code Scope -
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. -
൞ݎಞ 6. IntelliJ • Show Kotlin Bytecode ▶ Decompile •
Decompile ػ Java ٘۽ оמ -
• 2017֙ ୡࠗఠ Kotlin ҕࠗ (पઁ ҕࠗ 3~4ѐਘ) •
GDG Kotlin झఠ٣ ଵৈ • Kotlin ܐ (Kotlin Reference, Twitter, Medium, Qiita) • ࢎղ Kotlin झఠ٣ ೯ (ঌ۰Ҋ ղо ॶ۰Ҋ) ղо ೠ ೯ز -
Ә द೧ب ןঋইਃ • ઁ ݆ ࢎۈٜীѱ ҙबਸ ߉ӝ द
• ই ܐо ݅, Բળೞѱ Ӗٜ ৢۄ১ • ߄৬ э ࢎਊ೧ب غח • ٘о बೞѱ ࢿ оמ೧ • best practice ࠗ • և ٣ੋ ಁఢ ਊ оמ -
• ן ঋওযਃ • Kotlin ࣻ X, খਵ۽ Kotlin ழ
• Convert Java File to Kotlin File ۽ оߺѱ द • Show Kotlin Bytecode ۽ р оמ ରܳ ݅٘ח Action Item! -
ଵҊ • 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) -
Q&A GDG Slack #android #kotlin օ (Slack оੑ ݂ :
http://slack.gdg.kr/)