stanfy madcode meetup #10: from java to kotlin. from objective-c to swift
TRANSCRIPT
Stanfy MadCode #10From Java to Kotlin. From Objective-C to Swift.
by Oleksandr Tereshchuk and Alexander Voronov
Into New Language
Stanfy MadCode #10
Development Environment
Stanfy MadCode #10
[ ] IntelliJ IDEA 15 bundles the Kotlin
Stanfy MadCode #10
[ ] 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
[ ] Refactoring
Stanfy MadCode #10
[ ] Playground & REPL
https://developer.apple.com/swift/blog/?id=24
Stanfy MadCode #10
[ ] Backward Compatibility
Stanfy MadCode #10
[ ] Bridging & Interoperability
● Nullability
● Lightweight Generics
● Kindof
Stanfy MadCode #10
Project Design
Stanfy MadCode #10
[ ] Configure Android projectJust follow simple guide: https://kotlinlang.org/docs/tutorials/kotlin-android.html
Stanfy MadCode #10
[ ] Project Design
● Headers
● Modules
● Multiple Items per File
Stanfy MadCode #10
Language Vector
Stanfy MadCode #10
[ ] ConciseCreate 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
[ ] Safeval 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 ?: -1val len = x!!.length // Will throw if null. Rarely used.
Stanfy MadCode #10
[ ] 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
[ ] Protocol-Oriented
https://developer.apple.com/videos/play/wwdc2015-408
“Swift Is a Protocol-OrientedProgramming Language”
– Dave Abrahams
Stanfy MadCode #10
[ ] Values instead of References
VS
Stanfy MadCode #10
[ ] Safety
Stanfy MadCode #10
Language Features
Stanfy MadCode #10
[ ] Lambdasval longest = max(strings, { a, b -> a.length() < b.length() })
val lengthFive = strings.filter { it.length == 5 }.sortBy { it }.map { it.toUpperCase() }
Stanfy MadCode #10
[ ] Extension Methodsfun 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
[ ] Sealed classessealed 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
[ ] Data classesdata 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) = janeprintln("$name, $age years of age") // prints "Jane, 35 years of age"
Stanfy MadCode #10
[ ] Language Features
● First Class Functions
● Partial Application
● Generics
● Type Inference
● Enums
● Operators
● Standard Library
Stanfy MadCode #10
Publishing
Stanfy MadCode #10
[ ] Publishing
Stanfy MadCode #10
[ ] AppStore Publishing
Built with xcodebuild Exported from Xcode Organizer
100384 21680
Stanfy MadCode #10
Language Evolution
Stanfy MadCode #10
[ ] 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
[ ] Stable
Swift EvolutionStanfy MadCode #10
[ ] OpenSource
github.com/apple
swift.org
Stanfy MadCode #10
TODO
Stanfy MadCode #10
[ ] 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
[ ] Anko - https://github.com/Kotlin/anko
verticalLayout { val name = editText() button("Say Hello") { onClick { toast("Hello, ${name.text}!") } }}
Stanfy MadCode #10
[ ] TODO
bottomLeftButton.performSelector( “_whyIsThisViewNotFocusable”)
Stanfy MadCode #10
[ ] TODO
Stanfy MadCode #10
Thanks!
Stanfy MadCode #10
Happy Holidays!
Stanfy MadCode #10