Share
Embed
Transcript
51 slides
-
ࣗѐ • GDG Seoul • RGP Korea ਃӝਃ উ٘۽٘
ѐߊ • Է ݆ ѐߊо غ ࠶۽Ӓ - http://thdev.tech • Udemy ౣܽ ъ -
Kotlin ण ߑߨ જ ଼җ ݆ ޙࢲ ࠁӝ ӝࠄ ٘
ٮۄ ೞҊ, Convert ೧ࠁӝ թٜ જח ۄ࠳۞ܻ ഝਊ ٘ ӝ(let, apply, with, run, also ١) -
ೞ݅ Kotlin݅ णೞӝূ ই Javaܳ ݽܳ ࣽ হ ۞ ழ࠳о
ծ݅ ೧ೡࣻ۾ য۵ Kotlinਸ ખ ؊ ੜ ঌ۰ݶ Effective Java ೧ -
Ӓېب Ѥ ೧ೞݶ જ Lambdas Kotlin Extension Higher-Order function
3 о ೧ೞݶ Kotlin ഝਊࢿ ֫ইઉਃ -
Kotlin Extension Kotlinਸ णೞח न ঌҊ . let, run,
apply, with, also and more. -
Kotlin Extension public inline fun <T> T.apply(block: T.() -> Unit):
T { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } block() return this } // use val button = Button(this).apply { text = "Button" textSize = resources.getDimension(R.id.button_text_size) } -
Kotlin Extension public inline fun <T> T.apply(block: T.() -> Unit):
T { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } block() return this } // use val button = Button(this).apply { text = "Button" textSize = resources.getDimension(R.id.button_text_size) } Extension Higher-Order function Lambdas -
ੜ ഝਊ . let, apply, run ١ ݅ ੜ
ঌইب Kotlin য۵ ঋ Ө ղਊ աী ইоݶࢲ ൦. -
Kotlin Extension ٜ݅যࠁ 1 ױ҅ - String Emptyо ইפݶ
callback ߉ӝ interface Callback { fun notEmptyString(param: String) } fun notEmptyString(param: String?, callback: Callback) { if (!param.isNullOrEmpty()) { callback.notEmptyString(param!!) } } -
Kotlin Extension ٜ݅যࠁ 1 ױ҅ - String Emptyо ইפݶ callback
߉ӝ पઁ ࢎਊ notEmptyString(null, object : Callback { override fun notEmptyString(param: String) { // Use NotNull param } }) -
Kotlin Extensionۆ? Javaۢ UtilClassܳ ೞ ঋҊ, ё ഛਸ ઁҕ
StringUtil.notEmptyString() -> text.notEmptyString() functionҗ ۽ಌ౭(val/var) ݽف ഛਸ ઁҕ Generic ਊ оמ -
Kotlin Extension ٜ݅যࠁ 2 ױ҅ - Extension ਊ Receiver type
fun String?.notEmptyString(callback: Callback) fun notEmptyString(param: String?, callback: Callback) fun notEmptyString( String? . callback: Callback) -
Kotlin Extension ٜ݅যࠁ 2 ױ҅ - Extension ਊ fun String?.notEmptyString(callback:
Callback) { if (!this.isNullOrEmpty()) { callback.notEmptyString(this!!) } } Receiver object Receiver type -
Kotlin Extension ٜ݅যࠁ 2 ױ҅ - Extension ਊ notEmptyString(null, object
: Callback { override fun notEmptyString(param: String) { // Use Message } }) null , notEmptyString( . -
Kotlin Extension ٜ݅যࠁ 2 ױ҅ - Extension ਊ val name:
String? = "Taehwan" name.notEmptyString(object : Callback { override fun notEmptyString(param: String) { // Use Message } }) -
3 ױ҅ Kotlin Extension ਊ೮݅ ӭՔೞ ঋ. Listener??? Listener न
Higher-Order Functionਸ ࢎਊ೧ࠁ -
Higher-Order functionۆ? functionਸ parameter۽ ׳ೞѢա, return ೡ ࣻ functionਸ
۽ಌ౭ب(val/var) о ࣻ -
Higher-Order function ୡӝച val higherOrder: () -> Boolean = {
false } fun higherOrder(block: () -> String) { block() } -
Higher-Order function ୡӝച val higherOrder: () -> Boolean = {
false } Method return type Function initialization return value -
Higher-Order function ୡӝച val higherOrder: (param: String) -> Boolean =
{ false } Method Parameters param -> Kotlin lambdasח 1ѐ parameters ࢤۚ оמ > न it ఃਕ٘ ࢎਊ -
Higher-Order function ਊೞӝ ী val higherOrder: String?.() -> Boolean =
{ false } Method Receiver type val name: String? = "Taehwan" name.higherOrder() -
Higher-Order function ਊೞӝ 3 ױ҅ - Higher-Order function ਊ fun
String?.notEmptyString(callback: (String) -> Unit) { if (!this.isNullOrEmpty()) { callback(this!!) } } fun String?.notEmptyString(callback: Callback) { if (!this.isNullOrEmpty()) { callback.notEmptyString(this!!) } } -
Higher-Order function ਊೞӝ 3 ױ҅ - Higher-Order function ਊ val
name: String? = "Taehwan" name.notEmptyString({ println("notEmptyString $it") }) ( ) function ݄݃ Higher-Order functionীࢲ ࣗҚഐ()ח ࢤۚೡ ࣻ . -
Higher-Order function ਊೞӝ fun String?.notEmptyString( callback: (String) -> Unit, callback2:
() -> Unit) { if (!this.isNullOrEmpty()) { callback(this!!) } } "Taehwan".notEmptyString({ println("notEmptyString $it") }, { // something } , ) -
Kotlin extension ખ ؊ ࠁ public final void notEmptyString(@Nullable String
$receiver, @NotNull Function1 callback) { Intrinsics.checkParameterIsNotNull(callback, "callback"); CharSequence var3 = (CharSequence)$receiver; if (var3 != null && var3.length() != 0) { if ($receiver == null) { Intrinsics.throwNpe(); } callback.invoke($receiver); } } public final void notEmptyTest() { this.notEmptyString("Taehwan", (Function1)null.INSTANCE); } -
Kotlin extension ખ ؊ ࠁ public final void notEmptyString(@Nullable String
$receiver, @NotNull Function1 callback) { Intrinsics.checkParameterIsNotNull(callback, "callback"); CharSequence var3 = (CharSequence)$receiver; if (var3 != null && var3.length() != 0) { if ($receiver == null) { Intrinsics.throwNpe(); } callback.invoke($receiver); } } public final void notEmptyTest() { this.notEmptyString("Taehwan", (Function1)null.INSTANCE); } ೣࣻ ഐ ੌযաঠ ೡө? -
inline ഝਊ inlineਸ ୶о inlineਸ ୶оೣਵ۽ॄ kotlin extension ٘ܳ inline
೧ળ Ѿҗਵ۽ function ഐ ٘о ࢎۄ -
inline ഝਊ - ࢎਊೞח ٘ীࢲ this.notEmptyString("Taehwan", (Function1)null.INSTANCE); -> public final
void notEmptyTest() { String $receiver$iv = "Taehwan"; CharSequence var3 = (CharSequence)$receiver$iv; if (var3.length() != 0) { String var5 = "notEmptyString " + $receiver$iv; System.out.println(var5); } } -
DSLs (Domain-specific language) SQLীࢲ ൔೞѱ ࠅ ࣻ . Select …
/ Delete … ѾҴ খীࢲ ঠӝೠ 3оܳ ੜ ഝਊೞݶ DSLਸ औѱ ਊೡ ࣻ . -
inline fun Int.plus(param: Int) = this + param DSLs (Domain-specific
language) 1 plus 10 . (10) -
inline if (!this.isNullOrEmpty()) { callback(this!!) } } fun String?.notEmptyString(callback: String.()
-> Unit) { DSLs (Domain-specific language) “Taehwan" notEmptyString { println("notEmptyString $this") } . -
Kotlin Android KTX Jetpack family Android - Kotlin ࢎਊ ಞܳ
ذח extensions AndroidX ಁః۽ ઁҕ implementation ‘androidx.core:core-ktx:1.0.0-alpha1’ Android : https://developer.android.com/kotlin/ktx API : https://github.com/android/android-ktx API reference documentation : https://android.github.io/android-ktx/core-ktx/ Android Jetpack: sweetening Kotlin development with Android KTX (Google I/O '18) -
/** * Creates and shows a [Toast] with the given
[text] * * @param duration Toast duration, defaults to [Toast.LENGTH_SHORT] */ inline fun Context.toast(text: CharSequence, duration: Int = Toast.LENGTH_SHORT): Toast { return Toast.makeText(this, text, duration).apply { show() } } Use -> toast(“Message”) Kotlin Android KTX -
inline fun Context.toast(text: CharSequence, duration: Int = Toast.LENGTH_SHORT): Toast {
return Toast.makeText(this, text, duration).apply { show() } } infix ୶оೞݶ? messageо ੋ ҃ ৬ э ೡ ࣻ ೞ݅ durationب ਃೞݶ? inline fun Context.toast(message: () -> String) { toast(message()) } toast { "Message" } -
data class ToastData(val message: String, val duration: Int) class ToastDataBuilder
{ var message: String = "" var duration: Int = Toast.LENGTH_SHORT fun build(): ToastData = ToastData(message, duration) } message/duration ݽف ࢸೞ -
inline fun Context.toast(message: () -> String) { toast(message()) } message/duration
ݽف ࢸೞ inline fun Context.toast(body: ToastDataBuilder.() -> Unit) { val builder = ToastDataBuilder() builder.body() val item = builder.build() toast(item.message, item.duration) } -
toast { "Message" } message/duration ݽف ࢸೞ toast { message
= "Message" } toast { message = "Message" duration = Toast.LENGTH_SHORT } -
inline fun Context.toast(body: () -> Pair<String, Int>) { val (message,
duration) = body() toast(message, duration) } toast { “Message" to Toast.LENGTH_SHORT } Pair ਊ೧ࢲ ܻ? -
private val yourViewModel: YourViewModel by lazyInject // create Your ViewModel
YourViewModel(..., ..., ...) } ۠Ѫب ٜ݅ ࣻ . Ѧ ೞӝਤೠ ٘ח ড 30 ॳחҔীࢶ ױ 3 https://thdev.tech/LifecycleExtensions/