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

Stanfy MadCode 10: From Java to Kotlin, from Objective-C to Swift

Stanfy MadCode 10: From Java to Kotlin, from Objective-C to Swift

Oleksandr Voronov

January 11, 2016
Tweet

More Decks by Oleksandr Voronov

Other Decks in Programming

Transcript

  1. Stanfy MadCode #10 From Java to Kotlin. From Objective-C to

    Swift. by Oleksandr Tereshchuk and Alexander Voronov
  2. [ ] Command line tools - http://try.kotlinlang.org/ import java.io.File
 val

    folders = File(args[0]).listFiles { file -> file.isDirectory() }
 folders?.forEach { folder -> println(folder) } $ kotlinc -script list_folders.kts <path_to_folder_to_inspect> Stanfy MadCode #10
  3. [ ] 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) Want a singleton? Create an object: object ThisIsASingleton {
 val companyName: String = "JetBrains"
 } Stanfy MadCode #10
  4. [ ] Safe val x: String? = "Hi"
 x.length //

    Does not compile.
 
 val y: String = null // Does not compile.
 if (x != null) {
 x.length // Compiles! Not idiomatic just to get length!
 }
 
 // Same as above (IntelliJ auto-suggested the change).
 x?.length
 
 // Elvis operator.
 val len = x?.length ?: -1
 val len = x!!.length // Will throw if null. Rarely used. Stanfy MadCode #10
  5. [ ] Versatile & Interoperable 100% compatible with all JVM

    frameworks 100% Java Interoperability JavaScript compiler to target Node.js environment Very small runtime and stdlib for Android development ~ 1MB Stanfy MadCode #10
  6. [ ] Lambdas val longest = max(strings, { a, b

    -> a.length() < b.length() })
 
 val lengthFive = strings.filter { it.length == 5 }.sortBy { it }.map { it.toUpperCase() } Stanfy MadCode #10
  7. [ ] Extension Methods fun MutableList<Int>.swap(index1: Int, index2: Int) {


    val tmp = this[index1] // 'this' corresponds to the list
 this[index1] = this[index2]
 this[index2] = tmp
 }
 
 val l = mutableListOf(1, 2, 3)
 l.swap(0, 2) Stanfy MadCode #10
  8. [ ] Sealed classes sealed class Expr {
 class Const(val

    number: Double) : Expr()
 class Sum(val e1: Expr, val e2: Expr) : Expr()
 object NotANumber : Expr()
 } fun eval(expr: Expr): Double = when(expr) {
 is Const -> expr.number
 is Sum -> eval(expr.e1) + eval(expr.e2)
 NotANumber -> Double.NaN
 // the `else` clause is not required because we've covered all the cases
 } Stanfy MadCode #10
  9. [ ] Data classes data class User(val name: String =

    "", val age: Int = 0) /*
 * fun copy(name: String = this.name, age: Int = this.age) = User(name, age)
 */ val adam = User("Adam", 35) val jane = adam.copy(name = "Jane") 
 val (name, age) = jane
 println("$name, $age years of age") // prints "Jane, 35 years of age" Stanfy MadCode #10
  10. [ ] Language Features First Class Functions Partial Application Generics

    Type Inference Enums Operators Standard Library Stanfy MadCode #10
  11. [ ] AppStore Publishing Built with xcodebuild Exported from Xcode

    Organizer 10038 4 21680 Stanfy MadCode #10
  12. [ ] Still in beta... Sponsored and developed by JetBrains

    - https://kotlinlang.org/ Already have a huge community - Slack channel Not as known as Scala or Groovy yet Still in beta - syntax changes, missing features, hidden bugs Using Project Kotlin for Android by Jake Wharton Stanfy MadCode #10
  13. [ ] KAPT pain • Annotation processing is not fully

    functional yet ◦ Better Annotation Processing: Supporting Stubs in kapt • Mockito sometimes behaves wildly ◦ How to use Mockito with Kotlin? • New testing framework - Spek ◦ http://jetbrains.github.io/spek/ • Extension functions break incremental compilation ◦ All hopes for new beta Stanfy MadCode #10
  14. [ ] Anko - https://github.com/Kotlin/anko verticalLayout {
 val name =

    editText()
 button("Say Hello") {
 onClick { toast("Hello, ${name.text}!") }
 }
 } Stanfy MadCode #10