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

Kotlin Everywhere

Kotlin Everywhere

The popularity of Kotlin has exploded in the past year and Google's announcement of first-party support for Android at I/O means it's only going to keep increasing. Now is the perfect time to jump on the language, as its future in mobile and beyond is very bright.

In this talk our goal is to take a look at what Kotlin is, why you should use it, what are the benefits of using it and see where it is headed. You’ll walk away with introductory knowledge of Kotlin and how to start using it in various platforms.

Akshay Chordiya

October 07, 2017
Tweet

More Decks by Akshay Chordiya

Other Decks in Programming

Transcript

  1. Akshay Chordiya • Founder of BitFurther • Android Developer by

    heart • Active community speaker • Co-author of upcoming book “Kotlin Blueprints” • Graduated in 2016 @Aky @Akshay_Chordiya AkshayChordiya
  2. Hardik Trivedi • A Computer program writer • Works at

    Globant • Love writing tech blogs and contributing back to community • Mentor college graduates and professional who wants to go for mobile app development • Co-author of an upcoming book “Kotlin Blueprints” https://trivedihardik.wordpress.com/ hardik-trivedi
  3. Agenda • What’s Kotlin? • Why Kotlin? • Syntax •

    Future of Kotlin • Platform walkthrough • Conclusion • Q&A
  4. Kotlin • Modern & Statically typed • Targets JVM, Android,

    JavaScript and Native* • Works everywhere where Java works • 100% interoperable with Java™ • Combines Object Oriented and Functional features • Made by good folks at JetBrains • Open source * denotes it’s still in preview
  5. Why Kotlin • It's a fun language! • Tired of

    Java….JS • Helps you be more productive • Everywhere • Push from Google and Android community • Cool language features • Functional nature
  6. Variables // String (Implicitly inferred) val spell = "Lumos" //

    Int val number = 74 // Explicit type var spells: List<String> = ArrayList()
  7. “Use val for a value that won’t change” - Immutable

    val vs var “Use var for something that will vary with time.” - Mutable
  8. fun add(a: Int, b: Int): Int { return a +

    b } Functions fun add(a: Int, b: Int) = a + b fun add(a: Int = 0, b: Int = 0) = a + b
  9. // Compile time error val spell: String = null //

    OK val spell: String? = null Declaring null variables
  10. // Compile time error var spell: String = "Lumos" spell

    = null // OK var spell: String? = "Lumos" spell = null Even...
  11. // Safe val spell: String? = null val length =

    spell?.length // Safe with else val length = spell?.length ?: -1 Kotlin-ized way
  12. User class - Java public class User { private String

    firstName; private String lastName; public User() { this.firstName = ""; this.lastName = ""; } // Getter - Setter, hashCode, equals(), and other stuff @Override public String toString() { return "User{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + '}'; } } ~60 lines
  13. fun String.isEmail(): Boolean { // this -> value of string

    return Patterns.EMAIL_ADDRESS.matcher(this).matches() } Extension Function
  14. Smart Cast // Java way if (text instanceof String) {

    int length = ((String) text).length(); } // Kotlin way if (text is String) { // Smart casting val length = text.length }
  15. String interpolation fun sayHello(message: String) { println("Welcome $message") } fun

    sayTime() { println("Time: ${System.currentTimeMillis()}") }
  16. The idea • Android - Web - iOS teams will

    be able to work together, design together and solve problems together • Enable Code sharing • Easier development cycle with less hassle of maintaining multiple teams for each platform • Easy to incorporate in your existing project. Thanks to the interoperability
  17. Kotlin for Android • Jetbrains and Google, very much focused

    for Android. • It’s becoming more and more popular. • Philosophy and Spirit of Kotlin and Android are matching. • Android is enabled with excellent tooling support from JetBrains and Google. • Kotlin is perfect whose project still compiles on Java 6 and want to have fun with lucrative feature of Java 8 • It has 100% interoperability with Java from day one. One can start using Kotlin in Java project on any moment of the day. • It’s a mocktail with power of OOP language and beauty of functional programming language.
  18. Kotlin for Android • Android is smart and language is

    smarter • Kotlin has so much to offer for Android, it’s concise, expressive and powerful. • With the Null Safety Kotlin actually eliminates the possibility of app crash. • It’s concise, so it’s less line of code and less verbosity around the business logic. • Co routines and delegates reduces great amount of code • Anko is dedicated library from JetBrains which solves all Android problem • Android loves ceremonies of API • Lint also supports Kotlin.
  19. Kotlin for Android // Java Button myButton = findViewById(R.id.my_button); myButton.setText("Click");

    // Kotlin import kotlinx.android.synthetic.main.activity_home.* myButton.text="Click"
  20. // Java Editor editor = sharedpreferences.edit(); editor.putString("key", "value"); editor.apply(); Kotlin

    for Android // Kotlin private var isConnectedToTwitter: Boolean by DelegatedPreference(this, IS_CONNECTED_TO_TWITTER, false)
  21. • No support for statics • Extra runtime overhead •

    Adds around 6K method count • Increases the app size • Mocking is the problem Kotlin for Android
  22. Kotlin for JavaScript • Do not think support for JavaScript

    is on experimental basis. • All Koltin features are fully supported • Multiple ways to compile Koltin to Javascript • Koltin’s package for JavaScript includes libraries which supports DOM element manipulation, and also Graphical elements using WebGL
  23. Kotlin for JavaScript • Let’s understand how Koltin code gets

    converted in JavaScript import kotlin.browser.document fun main(args: Array<String>) { document.bgColor="FF0000" val message = "Hello JavaScript!" println(message) }
  24. Kotlin for JavaScript • Index.html will look like <!DOCTYPE html>

    <html lang="en"> <head> <title>Console Output</title> </head> <body> <script type="text/javascript"src="out/production/KotlinWeb/lib/kotlin.js"></script> <script type="text/javascript" src="out/production/KotlinWeb/KotlinWeb.js"></script> </body> </html>
  25. if (typeof kotlin === 'undefined') { throw new Error("Error loading

    module 'KotlinWeb'. Its dependency 'kotlin' was not found. Please, check whether 'kotlin' is loaded prior to 'KotlinWeb'."); } var KotlinWeb = function (_, Kotlin) { 'use strict'; var println = Kotlin.kotlin.io.println_s8jyv4$; function main(args) { document.bgColor = 'FF0000'; var message = 'Hello JavaScript!'; println(message); } _.main_kand9s$ = main; Kotlin.defineModule('KotlinWeb', _); main([]); return _; }(typeof KotlinWeb === 'undefined' ? {} : KotlinWeb, kotlin);
  26. • Koltinx.html is a library which has DSL (Domain Specific

    Language) to write HTML • You can build a DOM tree window.setInterval({ val myDiv = document.create.div("panel") { p { +"Here is " a("http://kotlinlang.org") { +"official Kotlin site" } } } document.getElementById("container")!!.appendChild(myDiv) document.getElementById("container")!!.append { div { +"added it" } } }, 1000L)
  27. • You can build HTML directly to System.out.appendHTML().html { body

    { div { a("http://kotlinlang.org") { target = ATarget.blank +"Main site" } } } }
  28. Going native • Kotlin without a VM! • Compiles down

    to machine code • Self-contained executable for iOS, macOS, Linux and Windows • It uses LLVM compiler • It’s still in preview! Not in a shippable state at all • Interops with C • It will be useful for building apps for iOS and embedded systems
  29. Further Resources • All the images and logos used are

    trademarks of respective companies. • Kotlin Official Website • Kotlin and Android • Getting started with Kotlin - Video • Sample News App with 100% Kotlin • Why Kotlin-ize your Android Development Medium - Akshay Chordiya • Kotlin - Goodbye findViewById • Easter Functions in Kotlin • Being more productive with Kotlin