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

Dont worry - switch to Kotlin

sergbek
March 13, 2017

Dont worry - switch to Kotlin

sergbek

March 13, 2017
Tweet

Other Decks in Education

Transcript

  1. Why … ? - “To invent the wheel” - Marketing

    - Сuriosity - “I do not like”
  2. Why do we need Kotlin? • Stuck on java 6

    (7) ◦ No streams ◦ No try-with-resources ◦ No lambdas, methods refs -> Use RxJava -> Use Retrolambda -> minSdkVersion = 19 or Retrolambda • Java language problems ◦ “Util Hell” ◦ Nullability problems ◦ Mutability problems ◦ General verbosity of common idioms
  3. Kotlin Features String templates Properties Lambdas Inline functions Data class

    Smart cast Null safety Default values for function parameters Lazy property Extension Functions Equals Single-expression functions When expression let, apply, use, with Collections Kotlin Android Extensions Plugin Anko
  4. Extension Functions fun showToast(context: Context, @StringRes idRes: Int){ Toast.makeText(context, idRes,

    Toast.LENGTH_LONG).show() } class MainActivity: AppCompatActivity() { fun showInfo() { showToast(this, R.string.error_network) } }
  5. Extension Functions fun Context.showToast(@StringRes idRes: Int){ Toast.makeText(this, idRes, Toast.LENGTH_LONG).show() }

    class MainActivity: AppCompatActivity() { fun showInfo() { // or this.showToast(R.string.error_network) showToast(R.string.error_network) } }
  6. Extension Functions fun Float.dpToPx(): Int { val metrics = Resources.getSystem().displayMetrics

    val px = this * (metrics.densityDpi / 160f) return Math.round(px) } fun Int.pxToDp(): Int { val metrics = Resources.getSystem().displayMetrics val dp = this / (metrics.densityDpi / 160f) return Math.round(dp) }
  7. Data class What it does ? Nothing. Just hold data

    What it provides ? - getters, setters - equals() / hashCode() - toString - copy() function
  8. Default values for function parameters Function parameters can have default

    values, which are used when a corresponding argument is omitted. This allows for a reduced number of overloads compared to other languages.
  9. Lazy property val preference = getSharedPreferences("pref") override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) val username = preference.getString("username") } crash, require context
  10. Lazy property val preference = getSharedPreferences("pref") override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) val username = preference.getString("username") }
  11. Lazy property val preference by lazy { getSharedPreferences("pref") } override

    fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val username = preference.getString("username") }
  12. Lazy property val validator: Validator by lazy { Validator(this) }

    button1.click { validator.validate() } button2.click { validator.validate()}
  13. let, apply, use, with Higher-order functions - function that takes

    functions as parameters, or returns a function.
  14. with inline fun <T: BaseInteractor> withInteractor(receiver: T, block: T.() ->

    Unit) = receiver.block() withInteractor(loginInteractor) { loginCustomer(password, username) .execute({ onLoginSuccess(it) }, { ErrorHandler.onError(router, it) }) }
  15. use (try with resources function) fun countUsers(): Long { val

    database = openDatabase() val result = database.count("users") database.close() return result } need to close
  16. use (try with resources function) fun countUsers(): Long { val

    database = openDatabase() val result = database.count("users") database.close() return result } fun countUsers() = openDatabase().use { it.count("users") } need to close
  17. use (try with resources function) fun countUsers(): Long { val

    database = openDatabase() val result = database.count("users") database.close() return result } fun countUsers() = openDatabase().use { it.count("users") } automatically close database
  18. Collections getOrElse() find() filter() filterNot() filterNotNull() flatMap() take() takeLast() sortBy()

    sumBy() sortByDescending() groupBy() map() mapNotNull() all() any() maxBy() minBy() minWith() zip() ….
  19. Kotlin Android Extensions Plugin // R.layout.activity_order_info import kotlinx.android.synthetic.main.activity_order_info.* public class

    OrderInfoActivity: AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_order_info) txtTitle.text = "Hello, Kotlin!" btnHello.setOnClickListener {...} } }
  20. Kotlin Android Extensions Plugin // R.layout.activity_order_info import kotlinx.android.synthetic.main.activity_order_info.* public class

    OrderInfoActivity: AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_order_info) txtTitle.text = "Hello, Kotlin!" btnHello.setOnClickListener {...} } } <Button android:id="@+id/btnHello" android:layout_width="match_parent" android:layout_height="wrap_content"/>
  21. Compatibility with Java - 100% - Kotlin can call any

    method from Java - Java can call any function from Kotlin - Add Kotlin to an existing Java project is very simple