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

Kotlin: A pragmatic programming language by JetBrains

Jigar Gosar
February 27, 2016

Kotlin: A pragmatic programming language by JetBrains

A pragmatic language for JVM and Android.
Combines OO and functional features.
Focused on interoperability, safety, clarity, tooling support.
Open Source.
Works everywhere where Java works.
Key focus on interoperability and seamless support for mixed Java+Kotlin projects.

Jigar Gosar

February 27, 2016
Tweet

Other Decks in Programming

Transcript

  1. Conventions used in this talk • Never == Almost Never

    • Always == Almost always • same goes for right, wrong, good, bad, 100%, 0%
  2. What is Kotlin? a pragmatic programming language for JVM and

    Android Combines OO and functional features Focused on • interoperability • safety • clarity • tooling support
  3. Kotlin works everywhere where Java works • Server-side applications •

    Mobile applications (Android) • Desktop applications • Works with all major tools and services such as ◦ IntelliJ IDEA, Android Studio and Eclipse ◦ Maven, Gradle and Ant ◦ Spring Boot ◦ GitHub, Slack and even Minecraft
  4. Is it ready for production? • Yes. And for quite

    some time. • JetBrains, have been using Kotlin in real-life projects on a rather extensive scale over the last two years. • Over 500K lines of Kotlin code in projects such as IntelliJ IDEA and Rider. • In addition, there are quite a few companies that have been using Kotlin in production for some time now.
  5. Open Source language • Developed on GitHub • Under Apache

    2.0 Open-Source license • Over 100 contributors to date • Adoption: 2 million+ Kotlin LOC on GitHub
  6. Concise Create a POJO with getters, setters, equals(), hashCode(), toString()

    and copy() in a single line: data class Customer(val name: String, val email: String, val company: String)
  7. Concise Or filter a list using a lambda expression: val

    positiveNumbers = list.filter {it > 0}
  8. SAFE Get rid of those pesky NullPointerExceptions, you know, The

    Billion Dollar Mistake var output : String output = null // compilation error And of course, Kotlin protects you from mistakenly operating on nullable types, including those from Java println(output.length()) // compilation error
  9. SAFE And if you check a type is right, the

    compiler will auto-cast it for you fun calculateTotal(obj: Any) { if (obj is Invoice) { obj.calculateTotal() } }
  10. Versatile Multi-purpose language allowing you to build any type of

    application • Android Development: No performance impact. Very small runtime. • JavaScript: Write code in Kotlin and target JavaScript to run on Node.js or in browser • Web: Whether you want strongly typed HTML, CSS builders or just plain web development. • Application Server ◦ The technology doesn't matter. 100% compatible with all JVM frameworks. • Enterprise: Use Kotlin for any type of Enterprise Java EE development.
  11. Create and consume Java code at will class NettyHandler: ChannelInboundMessageHandlerAdapter<Any>()

    { override fun messageReceived(p0: ChannelHandlerContext?, p1: Any?) { throw UnsupportedOperationException() } } Or use any existing library on the JVM, as there’s 100% compatibility, including SAM support. Interoperable
  12. Interoperable Target either the JVM or JavaScript. Write code in

    Kotlin and decide where you want to deploy to import js.dom.html.* fun onLoad() { window.document.body.innerHTML += "<br/>Hello, Kotlin!" }
  13. Functions fun sum(a: Int, b: Int): Int { return a

    + b } fun sum(a: Int, b: Int) = a + b fun printSum(a: Int, b: Int): Unit { print(a + b) }
  14. Variables val a: Int = 1 val b = 1

    // `Int` type is inferred val c: Int // Type required when no initializer // is provided c = 1 // definite assignment var x = 5 // `Int` type is inferred x += 1
  15. String Interpolation & docstring val i = 10 val s

    = "i = $i" // evaluates to "i = 10" val s = "abc" val str = "$s.length is ${s.length}" // evaluates to "abc.length is 3" val price = """ ${'$'}9.99 """
  16. Conditional Expression fun max(a: Int, b: Int): Int { if

    (a > b) return a else return b } fun max(a: Int, b: Int) = if (a > b) a else b
  17. When Expression when (x) { 1 -> print("x == 1")

    2 -> print("x == 2") 0, 1 -> print("x == 0 or x == 1") parseInt(s) -> print("s encodes x") in 1..10 -> print("x is in the range") in validNumbers -> print("x is valid") !in 10..20 -> print("x is outside the range") is String -> x.startsWith("prefix") x.isOdd() -> print("x is odd") x.isEven() -> print("x is even") else -> print("otherwise") }
  18. For Loop, while & do..while for (item in collection) print(item)

    for (i in array.indices) print(array[i]) for ((index, value) in array.withIndex()) { println("the element at $index is $value") }
  19. Collections val immutableList = listOf(1, 2, 3) val mutable =

    arrayListOf(1, 2, 3) val immutableMap = mapOf("foo" to 1, "bar" to 2) val readWriteMap = hashMapOf("foo" to 1, "bar" to 2) println(map["foo"])
  20. Kotlin’s type system is aimed to eliminate NullPointerException’s from our

    code. The only possible causes of NPE’s may be: • An explicit call to throw NullPointerException() • External Java code has caused it • There’s some data inconsistency with regard to initialization (an uninitialized this available in a constructor is used somewhere)
  21. null safety var a: String = "abc" a = null

    // compilation error val l = a.length // safe var b: String? = "abc" b = null // ok val l = if (b != null) b.length else -1 val l = b.length // error: variable 'b' can be null
  22. null safety - Smart Cast if (b != null &&

    b.length > 0) print("String of length ${b.length}") else print("Empty string")
  23. Smart Type Checks and Casts if (obj is String) {

    print(obj.length) // obj is automatically cast to String } if (obj !is String) { // same as !(obj is String) print("Not a String") } else { print(obj.length) }
  24. Smart Type Checks and Casts if (x !is String) return

    print(x.length) // x is automatically cast to String
  25. “Unsafe” cast operator val x: String = y as String

    // y can’t be null else CCE val x: String? = y as String? // y can’t be Int val x: String? = y as? String // y can be anything
  26. Extension Functions fun String.last(): Char { return this[lastIndex] } fun

    String.last() = this[lastIndex] val lastChar = "abc".last(); println("lastChar = ${lastChar}")
  27. Nullable Receiver fun Any?.toString(): String { if (this == null)

    return "null" return toString() // why does this compile? }
  28. More about Extension • Extensions are resolved statically • Extension

    Properties • Companion Object Extensions • Scope of Extensions • Declaring Extensions as Members https://kotlinlang.org/docs/reference/extensions.html
  29. Classes and Inheritance class Empty class Person(firstName: String) { init

    { logger.info("Customer initialized with value ${name}") } } class Person(val firstName: String, val lastName: String, var age: Int) { // ... }
  30. Classes and Inheritance open class A { open fun f()

    { print("A") } fun a() { print("a") } } interface B { // interface members are 'open' by default fun f() { print("B") } fun b() { print("b") } } class C() : A(), B { // The compiler requires f() to be overridden: override fun f() { super<A>.f() // call to A.f() super<B>.f() // call to B.f() } }
  31. Data Classes data class User(val name: String, val age: Int)

    data class User(val name: String = "", val age: Int = 0) • equals()/hashCode() pair, • toString() of the form "User(name=John, age=42)", • componentN() functions corresponding to the properties in their order of declaration, • copy() function (see below).
  32. Data Classes: Copying // generated by compiler for previous class

    fun copy(name: String = this.name, age: Int = this.age) = User(name, age) val jack = User(name = "Jack", age = 1) val olderJack = jack.copy(age = 2)
  33. Data Classes and Destructuring Declarations val jane = User("Jane", 35)

    val (name, age) = jane println("$name, $age years of age") // prints "Jane, 35 years of age"
  34. Generics class Box<T>(t: T) { var value = t }

    val box: Box<Int> = Box<Int>(1) val box = Box(1) // 1 has type Int, // so the compiler figures out that we are talking about Box<Int>
  35. Functions Infix Notation val a = 1 to 3 val

    b = "a".to("z") val c: Pair<String, String> = "a" to "z"
  36. Packages package foo.bar fun baz() {} class Goo {} ------------------------------------------

    import foo.Bar // Bar is now accessible without qualification import bar.Bar as bBar // bBar stands for 'bar.Bar' import foo.* // everything in 'foo' becomes accessible