Kotlin, a Year on
Mike Gouline
gouline.net | @mikestruct
What has changed?
Annotation processing (kapt)
Anko
Anko: Example
verticalLayout {
padding = dip(16)
textView("Username:") {
textSize = 18f
}.layoutParams { verticalMargin = dip(4) }
val login = editText()
button("Sign up") {
textSize = 20f
onClick { login(login.text) }
}.layoutParams { topMargin = dip(8) }
}
Anko: Example
Android extensions
Android extensions: Example
<TextView
android:id="@+id/hello"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
import kotlinx.android.synthetic.main.*
public class MyActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
this.hello.setText("Hi!")
}
}
Nullability
Sealed classes
sealed class Pet(val name: String) {
class Dog(name: String): Pet(name)
class Cat(name: String): Pet(name)
}
fun Pet.saySomething(): String {
return when (this) {
is Dog -> "woof"
is Cat -> "meow"
}
}
Miscellaneous
Thank you!
Resources: https://gouline.net/talks
Documentation: http://kotlinlang.org/docs/reference/
gouline.net | @mikestruct