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

D94c9a683d3b45cfe42548cd5a1a39b0?s=128

Alexander Voronov

January 11, 2016
Tweet

Transcript

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

    Swift. by Oleksandr Tereshchuk and Alexander Voronov
  2. Into New Language Stanfy MadCode #10

  3. Development Environment Stanfy MadCode #10

  4. [ ] IntelliJ IDEA 15 bundles the Kotlin Stanfy MadCode

    #10
  5. [ ] 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
  6. [ ] Refactoring Stanfy MadCode #10

  7. [ ] Playground & REPL https://developer.apple.com/swift/blog/?id=24 Stanfy MadCode #10

  8. [ ] Backward Compatibility Stanfy MadCode #10

  9. [ ] Bridging & Interoperability Nullability Lightweight Generics Kindof Stanfy

    MadCode #10
  10. Project Design Stanfy MadCode #10

  11. [ ] Configure Android project Just follow simple guide: https://kotlinlang.org/docs/tutorials/kotlin-android.html

    Stanfy MadCode #10
  12. [ ] Project Design Headers Modules Multiple Items per File

    Stanfy MadCode #10
  13. Language Vector Stanfy MadCode #10

  14. [ ] 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
  15. [ ] 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
  16. [ ] 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
  17. [ ] Protocol-Oriented https://developer.apple.com/videos/play/wwdc2015-408 “Swift Is a Protocol-Oriented Programming Language”

    – Dave Abrahams Stanfy MadCode #10
  18. [ ] Values instead of References VS Stanfy MadCode #10

  19. [ ] Safety Stanfy MadCode #10

  20. Language Features Stanfy MadCode #10

  21. [ ] 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
  22. [ ] 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
  23. [ ] 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
  24. [ ] 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
  25. [ ] Language Features First Class Functions Partial Application Generics

    Type Inference Enums Operators Standard Library Stanfy MadCode #10
  26. Publishing Stanfy MadCode #10

  27. [ ] Publishing Stanfy MadCode #10

  28. [ ] AppStore Publishing Built with xcodebuild Exported from Xcode

    Organizer 10038 4 21680 Stanfy MadCode #10
  29. Language Evolution Stanfy MadCode #10

  30. [ ] 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
  31. [ ] Stable Swift Evolution Stanfy MadCode #10

  32. [ ] OpenSource github.com/apple swift.org Stanfy MadCode #10

  33. TODO Stanfy MadCode #10

  34. [ ] 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
  35. [ ] Anko - https://github.com/Kotlin/anko verticalLayout {
 val name =

    editText()
 button("Say Hello") {
 onClick { toast("Hello, ${name.text}!") }
 }
 } Stanfy MadCode #10
  36. [ ] TODO bottomLeftButton.performSelector( “_whyIsThisViewNotFocusable” ) Stanfy MadCode #10

  37. [ ] TODO Stanfy MadCode #10

  38. Thanks! Stanfy MadCode #10

  39. Happy Holidays! Stanfy MadCode #10