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

Kotlin Features (GDG Ajah Kotlin Everywhere 2019)

Kotlin Features (GDG Ajah Kotlin Everywhere 2019)

As part of the Kotlin/Everywhere community-led events, aimed at exposing Kotlin as a programming language, this talk was created to explain Kotlin's unique/productivity-boosting features.

In this talk, you will get an overview of the Kotlin programming language - its syntax, and its unique features that helps to boost one's productivity while coding.

Chizoba Ogbonna

July 06, 2019
Tweet

More Decks by Chizoba Ogbonna

Other Decks in Programming

Transcript

  1. var name: String = “Chizoba” //This can't be null name

    = null var pictureUrl: String? = “some url” //You can keep a null here pictureUrl = null var age = 100 //The compiler assumes non-null age = null fun returnLength(notNull: String): Int { return notNull.length } returnLength(name) returnLength(pictureUrl)
  2. var name: String = “Chizoba” //This can't be null name

    = null var pictureUrl: String? = “some url” //You can keep a null here pictureUrl = null var age = 100 //The compiler assumes non-null age = null fun returnLength(notNull: String): Int { return notNull.length } returnLength(name) returnLength(pictureUrl)
  3. var name: String = “Chizoba” //This can't be null name

    = null var pictureUrl: String? = “some url” //You can keep a null here pictureUrl = null var age = 100 //The compiler assumes non-null age = null fun returnLength(notNull: String): Int { return notNull.length } returnLength(name) returnLength(pictureUrl)
  4. var name: String = “Chizoba” //This can't be null name

    = null var pictureUrl: String? = “some url” //You can keep a null here pictureUrl = null var age = 100 //The compiler assumes non-null age = null fun returnLength(notNull: String): Int { return notNull.length } returnLength(name) returnLength(pictureUrl)
  5. var name: String = “Chizoba” //This can't be null name

    = null var pictureUrl: String? = “some url” //You can keep a null here pictureUrl = null var age = 100 //The compiler assumes non-null age = null fun returnLength(notNull: String): Int { return notNull.length } returnLength(name) returnLength(pictureUrl)
  6. var name: String = “Chizoba” //This can't be null name

    = null var pictureUrl: String? = “some url” //You can keep a null here pictureUrl = null var age = 100 //The compiler assumes non-null age = null fun returnLength(notNull: String): Int { return notNull.length } returnLength(name) returnLength(pictureUrl)
  7. var name: String = “Chizoba” //This can't be null name

    = null var pictureUrl: String? = “some url” //You can keep a null here pictureUrl = null var age = 100 //The compiler assumes non-null age = null fun returnLength(notNull: String): Int { return notNull.length } returnLength(name) returnLength(pictureUrl)
  8. var name: String = “Chizoba” //This can't be null name

    = null var pictureUrl: String? = “some url” //You can keep a null here pictureUrl = null var age = 100 //The compiler assumes non-null age = null fun returnLength(notNull: String): Int { return notNull.length } returnLength(name) returnLength(pictureUrl)
  9. var name: String = “Chizoba” //This can't be null name

    = null var pictureUrl: String? = “some url” //You can keep a null here pictureUrl = null var age = 100 //The compiler assumes non-null age = null fun returnLength(notNull: String): Int { return notNull.length } returnLength(name) returnLength(pictureUrl)
  10. var name: String = “Chizoba” //This can't be null name

    = null var pictureUrl: String? = “some url” //You can keep a null here pictureUrl = null var age = 100 //The compiler assumes non-null age = null fun returnLength(notNull: String): Int { return notNull.length } returnLength(name) returnLength(pictureUrl) Null safety
  11. val date: ChronoLocalDate? = LocalDate.now() if (date != null) {

    println(date.isLeapYear) } if (date != null && date.isLeapYear) { println("It's a leap year!") } if (date is LocalDate) { val month = date.monthValue println(month) }
  12. val date: ChronoLocalDate? = LocalDate.now() if (date != null) {

    println(date.isLeapYear) } if (date != null && date.isLeapYear) { println("It's a leap year!") } if (date is LocalDate) { val month = date.monthValue println(month) }
  13. val date: ChronoLocalDate? = LocalDate.now() if (date != null) {

    println(date.isLeapYear) } if (date != null && date.isLeapYear) { println("It's a leap year!") } if (date is LocalDate) { val month = date.monthValue println(month) }
  14. val date: ChronoLocalDate? = LocalDate.now() if (date != null) {

    println(date.isLeapYear) } if (date != null && date.isLeapYear) { println("It's a leap year!") } if (date is LocalDate) { val month = date.monthValue println(month) } Smart Casts
  15. fun main() { val company = Company() val user =

    User(1, “[email protected]") user.email = "[email protected]" } class User(val id: Int, var name: String) class Company
  16. Data classes val newUser = user.copy(); val newUser = user.copy(10);

    val newUser = user.copy(name = “techietoby”);
  17. Data classes val newUser = user.copy(); val newUser = user.copy(10);

    val newUser = user.copy(name = “techietoby”);
  18. Data classes val newUser = user.copy(); val newUser = user.copy(10);

    val newUser = user.copy(name = “techietoby”);
  19. Data classes val newUser = user.copy(); val newUser = user.copy(10);

    val newUser = user.copy(name = “techietoby”);
  20. Sealed classes sealed class NetworkResponse data class Success(val result: String):

    NetworkResponse() data class Failure(val error: Error): NetworkResponse()
  21. Sealed classes sealed class NetworkResponse data class Success(val result: String):

    NetworkResponse() data class Failure(val error: Error): NetworkResponse()
  22. Sealed classes sealed class NetworkResponse data class Success(val result: String):

    NetworkResponse() data class Failure(val error: Error): NetworkResponse()
  23. Sealed classes sealed class NetworkResponse data class Success(val result: String):

    NetworkResponse() data class Failure(val error: Error): NetworkResponse()
  24. Sealed classes viewModel.data.observe(this, Observer<NetworkResponse> { data -> data ?: return@Observer

    // skip nulls when(data) { is Success -> showResult(data.result) // smart cast to Success is Failure -> showError(data.error) // smart cast to Failure } })
  25. object as a declaration object User { fun delete() {

    … } } fun main() { User.delete() }
  26. Companion object class ProfileActivity : AppCompatActivity() { companion object {

    fun start(activity: Activity) { startActivity(Intent(activity, ProfileActivity::class.java)) } } } Class HomeActivity: AppCompatActivity() { … ProfileActivity.start(this) }