Slide 1

Slide 1 text


 The Kotlin & Java Blend Bob Dahlberg - @mr_bob

Slide 2

Slide 2 text

Bob Dahlberg Isotop - Tech Lead Mobile
 Challenge Accepted - Founder Who’s Bob?
 - Building software
 - CrossFit Instructor
 - Soon to be father
 - Loves a good challenge!

Slide 3

Slide 3 text

Background Scala & Functional Fell in love with functional programming and Scala 6 years ago.. .. and tried to write Android Apps with it.. 


Slide 4

Slide 4 text

Background Scala & Functional Fell in love with functional programming and Scala 6 years ago.. .. and tried to write Android Apps with it.. ..it works!


Slide 5

Slide 5 text

Background Scala & Functional Fell in love with functional programming and Scala 6 years ago.. .. and tried to write Android Apps with it.. ..it works!
 But it ain’t pretty.

Slide 6

Slide 6 text

Background Scala & Functional Kotlin Found Kotlin two years ago. Tried to write Android Apps with it..

Slide 7

Slide 7 text

Background Scala & Functional Kotlin Works seamlessly It works! And it’s almost like it’s ment to be there!

Slide 8

Slide 8 text

Background Scala & Functional Kotlin Works seamlessly In production February 2016 Started mixing in kotlin classes in a feature branch late 2015. In February 2016 we went live with Kotlin classes.

Slide 9

Slide 9 text

And they all lived 
 happily ever after?

Slide 10

Slide 10 text

Blending Don’t be to eager to update and have the latest version of everything. They all have to sync. Only minor hiccups around version updates:
 - JDK - Kotlin - Android Support libs - Gradle - 3rd Party - Annotations

Slide 11

Slide 11 text

Official Android Language Android Studio 3.0 (canary 3) available since early June. Will all hiccups magically disappear? Plausible! It will at least be fewer problems with syncing since Android Studio will bundle everything in a controlled manner.

Slide 12

Slide 12 text

Official Android Language Android Studio 3.0 (canary 3) available since early June. Will all hiccups magically disappear? Plausible! It will at least be fewer problems with syncing since Android Studio will bundle everything in a controlled manner. Unless you use the early 
 access builds… ;)

Slide 13

Slide 13 text

Alternatives Java 8 Jack and api <= 23 Android 7 (api 24)
 has some Java 8 support “To test lambda expressions, method references, and type annotations on earlier versions of Android, go to your build.gradle file, and set compileSdkVersion and targetSdkVersion to 23 or lower. You will still need to enable the Jack toolchain to use these Java 8 features.”

Slide 14

Slide 14 text

Alternatives Java 8
 Jack and api <= 23 Retrolambda Absolutely - but kotlin is so much more than lambdas and method reference.

Slide 15

Slide 15 text

Let’s see some code!

Slide 16

Slide 16 text

The Good Nullable var name:String? = “Bob” // name can be null 
 var family:String = “Dahlberg” // family can never be null

Slide 17

Slide 17 text

The Good Nullable & Null safety fun release( dbs:List ) { dbs.forEach { it?.close() }
 } release( listOf( db1, null, db2 ) )

Slide 18

Slide 18 text

The Good Nullable & Null safety var tmp:File? = null /* lots of code later */ tmp?.let{ file -> prinln( “Removing ${file.name}” ) file.delete() }


Slide 19

Slide 19 text

The Good Nullable & Null safety Named params hide( true ) 


Slide 20

Slide 20 text

The Good Nullable & Null safety Named params hide( true ) fun hide( animate:Boolean ) { // Implement…
 }

Slide 21

Slide 21 text

The Good Nullable & Null safety Named params hide( animate = true ) fun hide( animate:Boolean ) { // Implement…
 }

Slide 22

Slide 22 text

The Good Nullable & Null safety Named params Type Alias typealias Ref = KClass typealias AliasMap = MutableMap val aliases:AliasMap fun find( alias:Int ):Ref? { return aliases[alias] }

Slide 23

Slide 23 text

The Good Nullable & Null safety Named params Type Alias typealias Predicate = (T) -> Boolean val even:Predicate = { it % 2 == 0 } listOf( 1, 2, 3 ).filter( even )

Slide 24

Slide 24 text

The Good Nullable & Null safety Named params Type Alias When & With when( x ) { is Int -> println( "X is int" ) is String -> println( "X is String" ) else -> println( "Can't find x..." ) } when { month >= 4 -> println( "jan-apr" ) month in 5..8 -> println( "may-aug" ) month <= 9 -> println( "sep-dec" ) }

Slide 25

Slide 25 text

The Good Nullable & Null safety Named params Type Alias When & With with( textView ) { setAllCaps( true ) setLines( 3 ) hint = "Add text" }

Slide 26

Slide 26 text

The Good Nullable & Null safety Named params Type Alias When & With Modern list handling
 listOf( 1, 2, 3, 4, 5 ) .filter { it > 3 } .map { it * 2 } .first()

Slide 27

Slide 27 text

The Good Nullable & Null safety Named params Type Alias When & With Modern list handling
 Extension Functions fun Int.squared() { 
 this * this } val Int.half get() = this / 2 3.squared() // 9 4.half // 2

Slide 28

Slide 28 text

The Bad Calling not null params from Java // kotlin class Kotlin { fun send( message:String ){ // message is not nullable so we // don’t handle nulls } } // java public class Java { private String name; public Java( Kotlin instance ) { // No compile error instance.send( name ); } }

Slide 29

Slide 29 text

The Bad Calling not null params from Java Extensions in data binding If a view uses google data binding, and the view model has extension functions. Those extension functions can’t be called.. yet?

Slide 30

Slide 30 text

The Bad Calling not null params from Java Extensions in data binding Refactoring (getting better) When refactoring kotlin code via Studio that is referenced from Java doesn’t always work as expected.

Slide 31

Slide 31 text

The Bad Calling not null params from Java Extensions in data binding Refactoring (getting better) Quote “It’s not as fun to write code in Java when you get used to the functionality from Kotlin.”

Slide 32

Slide 32 text

The Ugly Object call from java object App { val version = “2.1” } // java App.INSTANCE.getVersion()

Slide 33

Slide 33 text

The Ugly Object call from java class MyActivity { companion object { val TAG = “MyActivity” } } // java MyActivity.Companion.getTAG()

Slide 34

Slide 34 text

The Ugly Object call from java class MyActivity { companion object { @JvmField val TAG = “MyActivity” } } // java MyActivity.TAG

Slide 35

Slide 35 text

The Ugly Object call from java Extensions from Java // In file Extensions.kt fun Int.squared() { 
 this * this } // java ExtensionsKt.squared( 4 );

Slide 36

Slide 36 text

The Ugly Object call from java Extensions from Java // In file Extensions.kt @file:JvmName(“Ext”) fun Int.squared() { 
 this * this } // java Ext.squared( 4 );

Slide 37

Slide 37 text

The Ugly Object call from java Extensions from Java !! // They are there to be ugly… fun forceReplace( name:String? ):String { return name!!.replace( “_", “ " ) }

Slide 38

Slide 38 text

The Ugly Object call from java Extensions from Java !! Auto-convert It’s getting a lot better but not good. You’ve got the foundation but then you should really refactor it. - Don’t make data classes for you - A lot of !! and ? to reflect the java code but often you can alter that - Copy paste from one language into the other usually works from Java to Kotlin but not the other way around.

Slide 39

Slide 39 text

The Ugly Object call from java Extensions from Java !! Auto-convert Anonymous functions val listener = object:OnClickListener{
 override fun onClick( v:View? ) { // Implement…
 }
 }

Slide 40

Slide 40 text

The Ugly Object call from java Extensions from Java !! Auto-convert Anonymous functions val listener = { view -> // implement
 }

Slide 41

Slide 41 text

The Awesome Smart cast override fun onClick( v:View? ) { if( v != null ) { v.visibility = View.GONE // No question mark needed }
 }

Slide 42

Slide 42 text

The Awesome Smart cast val hasValue = when( expr ) {
 is String -> expr.isNotEmpty()
 is Int -> expr > 0 is IntArray -> expr.sum() > 0 else -> false
 }

Slide 43

Slide 43 text

The Awesome Smart cast fun send( msg:String? ) {
 if( msg != null && msg.isNotBlank() ) { // do send it }
 }

Slide 44

Slide 44 text

The Awesome Smart cast Delegates interface Engine { fun gas() fun brake() } class Turbo(val topSpeed:Int):Engine { var speed = 0 private set override fun gas() { speed = topSpeed } override fun brake() { speed = 0 } } class Car( e:Engine ):Engine by e val turboCar = Car( Turbo( 300 ) ) turboCar.gas()

Slide 45

Slide 45 text

The Awesome Smart cast Delegates class Presenter() { val name by lazy { findViewById( R.id.name as TextView ) } }

Slide 46

Slide 46 text

The Awesome Smart cast Delegates Data Classes data class Person( val name:String, val age:Int ) val bob = Person( “Bob”, 33 ) val youngBob = bob.copy( age = 15 ) print( bob ) // Person(name=Bob, age=33)

Slide 47

Slide 47 text

The Awesome Smart cast Delegates Data Classes Deconstructing data class Person( val name:String, val age:Int ) val bob = Person( “Bob”, 33 ) // Deconstructing val (name,age) = bob

Slide 48

Slide 48 text

The Awesome Smart cast Delegates Data Classes Deconstructing typealias Point = Pair fun find():Point { // do some calculations return Point( 9, 6 ) } val (x,_) = find()

Slide 49

Slide 49 text

The Awesome Smart cast Delegates Data Classes Deconstructing Operator overloading data class Person( val name:String, val age:Int ) val bob = Person( “Bob”, 33 ) operator fun Person.plus( inc:Int ) = copy( age = age + inc ) operator fun Person.plus( n:String ) = copy( name = “$name $n” ) bob + 2 // Person(name=Bob, age=35) bob + “D” // Person(name=Bob D, age=33)

Slide 50

Slide 50 text

The Awesome Smart cast Delegates Data Classes Deconstructing Operator overloading a + b // a.plus(b) a - b // a.minus(b) a % b // a.rem(b) a..b // a.rangeTo(b) a += b // a.plusAssign(b) a in b // b.contains(a) a !in b // !b.contains(a) a > b // a.compareTo(b) > 0 a == b // a?.equals(b) ?: (b === null) a() // a.invoke()

Slide 51

Slide 51 text

The Awesome Smart cast Delegates Data Classes Deconstructing Operator overloading And lots more… infix, default values, lambdas, type inference, method reference, inline functions, objects, interface functions, companion objects, interface properties, ranges, _, getters and setters…

Slide 52

Slide 52 text

Questions?

Slide 53

Slide 53 text

Bob Dahlberg @mr_bob - twitter @dahlberg.bob - medium.com bobdahlberg - speakersdeck.com Thanks! speakerdeck.com/bobdahlberg/the-kotlin-and-java-blend