Presentation for Android Developers about why they should consider using Kotlin. Key features of the language are discussed in comparison with Java. #LanguageChoiceMatters
http://blog.jimbaca.com
20. Elvis Operator
var address = book?.publisher?.address
var address = book?.publisher?.address ?:
"3300 N Interstate 35, Austin, TX"
21. Late Init
class MainActivity : AppCompatActivity() {
lateinit var book : Book
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
book.edition
}
}
⚠ 💥
lateinit property book has not been initialized
24. Smart Casting
if (x instanceof String) {
print(((String) x).length());
}
if (!(x instanceof String)) {
return;
}
print(((String) x).length());
if (x is String) {
print(x.length)
}
if (x !is String)
return
print(x.length)
25. Smart Casting
if (x !is String || x.length == 0) return
if (x is String && x.length > 0)
when (x) {
is Int -> print(x + 1)
is String -> print(x.length + 1)
is IntArray -> print(x.sum())
}
31. Interface
interface Book {
val author: String
val title: String
get() = “Wizard of Oz"
fun printAuthor() {
print(author)
}
}
class TechBook : Book {
override val author: String = "Howard Lee"
}
32. Interface Conflicts
interface A {
fun foo() { print("A") }
}
interface B {
fun foo() { print("B") }
fun bar() { print("bar") }
}
class D : A, B {
// foo?
}
33. Interface Conflicts
interface A {
fun foo() { print("A") }
}
interface B {
fun foo() { print("B") }
fun bar() { print("bar") }
}
class D : A, B {
override fun foo() {
super<A>.foo()
super<B>.foo()
}
35. Extension Functions
fun View.applyDimension(
value: Float,
type : Int = TypedValue.COMPLEX_UNIT_DIP
): Float{
val metrics = getResources().getDisplayMetrics()
val resultPix = TypedValue.applyDimension(type, value, metrics)
return resultPix
}
36. Extension Functions
class CustomView : View {
constructor(context: Context?) : super(context){
initialize()
}
…
fun initialize(){
val verticalLineThickness = applyDimension(5f)
val horizontalLineThickness = applyDimension(10f)
val paddingBetweenItems = applyDimension(15f)
val fontTitleSize = applyDimension(32f, TypedValue.COMPLEX_UNIT_SP)
val subtitleSize = applyDimension(14f, TypedValue.COMPLEX_UNIT_SP)
}
}
37. Extension Functions
fun TextView.setTextSize(value: Float,
type: Int = TypedValue.COMPLEX_UNIT_SP):
Float {
val res = getResources()
val metrics = res.getDisplayMetrics()
val resultPix = TypedValue.applyDimension(
type, value, metrics)
return resultPix
}
public void setTextSize(int unit, float size)
38. Extension Functions
Conflicts
fun TextView.setTextSize(value: Float,
type: Int = TypedValue.COMPLEX_UNIT_SP):
Float {
val res = getResources()
val metrics = res.getDisplayMetrics()
val resultPix = TypedValue.applyDimension(
type, value, metrics)
return resultPix
}
setTextSize(15.8f)
public void setTextSize(float size)
44. Returning Multiple Items
val (authors, datePublished) = getBook()
val authors = book.component1()
val datePublished = book.component2()
componentN() 🔑 operator
data class 🎉
45. data class
data class Book(
var authors: ArrayList<Author>,
var publication: Date,
var title: String,
var publisher: Publisher,
var isbn: String,
var asin: String,
var bookCover: Uri
)
public class Book {
ArrayList<Author> authors;
Date publication;
String title;
Publisher publisher;
String isbn;
String asin;
Uri bookCover;
public ArrayList<Author> getAuthors() {
return authors;
}
public void setAuthors(
ArrayList<Author> authors) {
this.authors = authors;
}
public Date getPublication() {
return publication;
}
. . .
BARF
56. Class Delegation
interface Base {
void print()
}
class BaseImpl extends Base {
int x;
public BaseImpl(int x){
this.x = x;
}
@Override
void print() {
print(x);
}
}
class Derived extends Base{
Base delegate;
@Override
void print(){
delegate.print();
}
void setDelegate(Base b){
delegate = b;
}
}
BaseImpl imp = new BaseImp(10);
Derived derived = new Derived();
derived.setDelegate(imp);
derived.print();
57. Class Delegation
interface Base {
fun print()
}
class BaseImpl(val x: Int) : Base {
override fun print() { print(x) }
}
class Derived(b: Base) : Base by b
val b = BaseImpl(10)
Derived(b).print()