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

Kotlinを使って思った10のコト fukuoka.kt #1

Kotlinを使って思った10のコト fukuoka.kt #1

chocomelonchan

July 05, 2017
Tweet

More Decks by chocomelonchan

Other Decks in Programming

Transcript

  1. 1. Extension͕͢͹Β͍͠ fun ImageView.setImageUrl(imageUrl: String?) { if (imageUrl == null

    || !URLUtil.isValidUrl(imageUrl)) { return } Glide.with(context).load(imageUrl).into(this) }
  2. 2. ؔ਺ܕ௒࠷ߴ͔Α arrayOf(1, 2, 3, 4, 5) // 1, 2,

    3, 4, 5 .filter { it % 2 == 0 } // 2, 4 .map {it * 2 } // 4, 8 .sum() // 12
  3. 3. σϑΥϧτ஋Ҿ/਺͕ศར fun hoge(fugaString: String, fooInt: Int = 0, barBoolean:

    = true) { // Do something } val fuga: Int = optionalVal1 ?: 5
  4. 4. when͕εϚʔτ viewModel?.getDataSourceState()?.observe(this, Observer { when (it) { DataSourceState.SUCCESS ->

    infoView.setType(InfoView.Type.HIDE) DataSourceState.LOADING -> infoView.setType(InfoView.Type.LOADING) DataSourceState.EMPTY_DATA -> infoView.setType(InfoView.Type.NOTFOUND) DataSourceState.ERROR -> infoView.setType( InfoView.Type.ERROR, View.OnClickListener { reload() }) } })
  5. 5. SmartCast͕ྑ͍ fun hoge(optionalVal1: String?, optionalVal2: String?) { if (optionalVal1

    != null && optionalVal2 != null) { val hoge = optionalVal1 + optionalVal2 // Do something } } if (object is Person) { print(object.name) }
  6. 6. είʔϓؔ਺͕ศར class HogeActivity: AppCompatActivity() { companion object { val

    BUNDLE_KEY_HOGE_INT = "HOGE_INT" fun createIntent(context: Context, hogeInt: Int): Intent = Intent(context, HogeActivity::class.java).apply { putExtra(BUNDLE_KEY_HOGE_INT, hogeInt) } } }
  7. 7. let let ͕ຊ౰ʹϜζ͍ inline fun <S, T, R> let(s:

    S?, t: T?, block: (s: S?, t: T?) -> R) { if (s != null && t != null) { block(s, t) } } let(optionalVal1, optionalVal2) { optionalVal1, optionalVal2 -> // Do something }