a deep dive into kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · kotlin • an...

34
1 A Deep Dive Into Kotlin By 小黑屋

Upload: others

Post on 16-Oct-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

1

A Deep Dive Into Kotlin

By 技术小黑屋

Page 2: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

2

About me

• 技术小黑屋(droidyue.com)博主• @Flipboard China(红板报)• GDG线下分享讲师

Page 3: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

4

Kotlin • An official language for Android recently• Powered by Jetbrains

Page 4: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

5

Why Kotlin

• Concise• Safe• interoperable• tool-friendly

Page 5: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

6

But•How to interoperate with Java•How to write the idiomatic Kotlin code•What are the pitfalls and how to avoid them•The common best practices

Page 6: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

7

Null Safety//nullable and non-null are two types //to make it possible to check nullability at compile timevar id: String = ""id = null //compile error

var title: String? = ""title = null

Page 7: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

8

We have less NPEsBut we could not avoid them

Page 8: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

9

!! is dangerousfun test(value: String?) { value?.length value!!.length // treat the value as non-null}

Page 9: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

10

Never use multiple !!user.contactInfo!!.emailContacts!!.primaryEmail//Exception in thread "main" kotlin.KotlinNullPointerException

//a better wayuser.contactInfo?.emailContacts?.primaryEmail

Page 10: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

11

!! in collection filteringfun getItemNames(items: List<Item?>) { items.filter { it != null }.map { it!!.title // not smart here, we need to recheck nullability }

//If we change the filter criteria, we may encounter npe

items.filterNotNull().map { it.title //smart here }}

Page 11: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

12

Look out for Platform types

• Any reference in Java may be null • It’s not practical to keep null-safety for these from Java • Types of Java declarations are treated specially in Kotlin and called platform types

Page 12: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

13

• It’s described as Type! such as String! • T! means "T or T?” • (Mutable)Collection<T>! means "Java collection of T may be

mutable or not, may be nullable or not" • But if the variable annotated with Nullability

annotations(Nullable,NonNull,etc), they are no longer platform types any more.

Page 13: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

14

Solutions

• Use annotations for Java variables and functions • It would be better to treat T! as T? for safety

Page 14: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

15

// In JavaMain.javapublic static List<String> getList() { String[] seasons = {"Spring, Summer, Autumn, Winter"}; return Collections.unmodifiableList(Arrays.asList(seasons));}

//Kotlin codefun mutableListPlatformTypes() { //wrong way // NPE or UnsupportedOperationException JavaMain.getList().add("Kotlin")

val list = mutableListOf<String>() JavaMain.getList()?.let {//maybe null list.addAll(it) //use a new list } list.add("Kotlin")}

Page 15: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

16

Check for deserialization

//need to check null(especially data from servers) data class IPInfoResponse(val status: Int, val ip: String) { fun isValid(): Boolean { return status == 0 && !TextUtils.isEmpty(ip) }}

Page 16: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

17

Prefer val to var• val is read-only • var is readable and writable

Page 17: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

18

fun getItemTitle(item: Item) { var title: String? if (item.isValid()) { title = item.title } else { title = "Not found" }

//the var title may be re-assigned to other value //value could be returned through if/else clause val title2 = if (item.isValid()) { item.title } else { "Not Found" }

}

Page 18: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

19

val item = getItem(position)//value returned by when clausereturn when(item?.type) { "hotmix" -> ITEM_TYPE_HOTMIX "promoted" -> ITEM_TYPE_PROMOTED "list" -> ITEM_TYPE_ITEM_LIST "sudoku" -> ITEM_TYPE_SUDOKU else -> ITEM_TYPE_SEARCHBOX}

Page 19: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

20

Object• It’s quick to implement Singleton • Companion object stores properties and functions of Class • Do not use Object to store static methods, Use the top-level functions

Page 20: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

21

class SSOServiceManager { companion object { private val APPID = "" private val SSO_SCOPE = "get_simple_userinfo" val RESULT_OK = -1 val instance = QQServiceManager() }}

Page 21: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

22

Standard.kt• let • apply • with • run

Page 22: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

23

//let is a scoping function DbConnection.getConnection().let { connection ->}// connection is no longer visible here

//let is also an alternative for null testingitem?.let { //code here}

Let

Page 23: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

24

Apply//without applyval intentFilter = IntentFilter()intentFilter. addAction(Intent.ACTION_PACKAGE_ADDED)intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED)intentFilter.addDataScheme("package")

//Use apply to group object initialization statements to allow for cleaner, easier to read code.IntentFilter().apply { addAction(Intent.ACTION_PACKAGE_ADDED) addAction(Intent.ACTION_PACKAGE_REMOVED) addDataScheme("package")}

Page 24: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

25

IntentFilter().apply { addAction(Intent.ACTION_PACKAGE_ADDED) addAction(Intent.ACTION_PACKAGE_REMOVED) addDataScheme("package")}.let { //use let to do the non-initialization work context.applicationContext.registerReceiver(packageBroadcastReceiver, it)}

Page 25: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

26

Withval w = Window()with(w) { setWidth(100) setHeight(200) setBackground(RED)}

Page 26: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

27

Run//run it's a combination of let and apply.

val a = "HelloWorld".replace("World","")a.run { println(length)}

Page 27: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

28

Inline

• Inline is a method of optimization(less methods) • It’s not the JIT inline

Page 28: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

29

inline fun <T> T?.runIfNotNull(block: (T) -> Unit): T? { if (this != null) { block(this) } return this}

fun testInline(arg: Any?) { println("testInlineStart") arg.runIfNotNull { println("arg is not null") } println("testInlineStop")}

//after inlinedfun testInline(arg: Any?) { println("testInlineStart") if (arg != null) { println("arg is not null") } println("testInlineStop")}

Page 29: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

30

Inline scenario

• Inline works best for functions with lambda parameter(avoid inner class) • If a little method is often called, You can inline it. • Too many inline functions would increase the compiler’s pressure

Page 30: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

31

Annotations

• @JvmStatic mark elements as static • @JvmField mark val/var as Java field

Page 31: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

32

object SocialShareHelper { @JvmField val TARGET_WECHAT = 1

val TARGET_WEIBO = 2}

int target = getShareTarget(); switch (target) { case SocialShareHelper.TARGET_WECHAT: break; case SocialShareHelper.INSTANCE.getTARGET_WEIBO():// error Kotlin’s pitfall break;}

Page 32: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

33

Prefer equal to ==

• In Kotlin, == is used to check Strings equality • But it will be confused to use it in a mixed project

Page 33: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

34

No Checked Exceptions//Checked Exceptions decreased productivity and little or no increase in code quality.//we could try-catch exceptions depending on our experience.fun PackageManager.isPackageExisting(targetPkg: String): Boolean { return try { getPackageInfo(targetPkg, PackageManager.GET_META_DATA) true } catch (e: PackageManager.NameNotFoundException) { e.printStackTrace() false }}

Page 34: A Deep Dive Into Kotlinbos.itdks.com/4b9b97a8050a4510921f8e745ff9f3ae.pdf · Kotlin • An official language for Android recently • Powered by Jetbrains. 5 Why Kotlin • Concise

35

Gifts