Kotlin is like Scala
BASICS
Hello World
Kotlin
println("Hello, world!")
Scala
println("Hello, world!")
Variables And Constants
Kotlin
val explicitDouble: Double = 70.0
Scala
val explicitDouble: Double = 70.0
Type Coercion
Kotlin
val label = "The width is "
val width = 94
val widthLabel = label + width
Scala
val label = "The width is "
val width = 94
val widthLabel = label + width
String Interpolation
Kotlin
val apples = 3
val oranges = 5
val fruitSummary = "I have ${apples + oranges} " +
                   "pieces of fruit."
Scala
val apples = 3
val oranges = 5
val fruitSummary = s"I have ${apples + oranges} " +
                   " pieces of fruit."
Range Operator
Kotlin
val names = arrayOf("Anna", "Alex", "Brian", "Jack")
val count = names.count()
for (i in 0..count - 1) {
    println("Person ${i + 1} is called ${names[i]}")
}
// Person 1 is called Anna
// Person 2 is called Alex
// Person 3 is called Brian
// Person 4 is called Jack
Scala
val names = Array("Anna", "Alex", "Brian", "Jack")
val count = names.length
for (i <- 0 until count) {
    println(s"Person ${i + 1} is called ${names(i)}")
}
// Person 1 is called Anna
// Person 2 is called Alex
// Person 3 is called Brian
// Person 4 is called Jack`
Inclusive Range Operator
Kotlin
for (index in 1..5) {
    println("$index times 5 is ${index * 5}")
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25
Scala
for (index <- 1 to 5) {
    println(s"$index times 5 is ${index * 5}")
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25
BASICS
Arrays
Kotlin
val shoppingList = arrayOf("catfish", "water",
    "tulips", "blue paint")
shoppingList[1] = "bottle of water"
Scala
var shoppingList = Array("catfish", "water", "tulips", "blue paint")
shoppingList(1) = "bottle of water"
Maps
Kotlin
val occupations = mutableMapOf(
    "Malcolm" to "Captain",
    "Kaylee" to "Mechanic"
)
occupations["Jayne"] = "Public Relations"
Scala
var occupations = scala.collection.mutable.Map(
    "Malcolm" -> "Captain",
    "Kaylee" -> "Mechanic"
)
occupations("Jayne") = "Public Relations"
Empty Collections
Kotlin
val emptyArray = arrayOf()
val emptyMap = mapOf()
Scala
val emptyArray = Array[String]()
val emptyDictionary = Map[String, Float]()
FUNCTIONS
Functions
Kotlin
fun greet(name: String, day: String): String {
    return "Hello $name, today is $day."
}
greet("Bob", "Tuesday")
Scala
def greet(name: String, day: String): String = {
    return s"Hello $name, today is $day."
}
greet("Bob", "Tuesday")
Tuple Return
Kotlin
data class GasPrices(val a: Double, val b: Double,
     val c: Double)
fun getGasPrices() = GasPrices(3.59, 3.69, 3.79)
Scala
def getGasPrices(): (Double, Double, Double) = {
    return (3.59, 3.69, 3.79)
}
Variable Number Of Arguments
Kotlin
fun sumOf(vararg numbers: Int): Int {
    var sum = 0
    for (number in numbers) {
        sum += number
    }
    return sum
}
sumOf(42, 597, 12)

// sumOf() can also be written in a shorter way:
fun sumOf(vararg numbers: Int) = numbers.sum()
Scala
def sumOf(numbers: Int*): Int = {
    var sum = 0
    for (number <- numbers) {
        sum += number
    }
    return sum
}
sumOf(42, 597, 12)

// Idiomatic Scala
Array(42, 597, 12).sum
Function Type
Kotlin
fun makeIncrementer(): (Int) -> Int {
    val addOne = fun(number: Int): Int {
        return 1 + number
    }
    return addOne
}
val increment = makeIncrementer()
increment(7)

// makeIncrementer can also be written in a shorter way:
fun makeIncrementer() = fun(number: Int) = 1 + number
Scala
def makeIncrementer(): Int => Int = {
    def addOne(number: Int): Int = {
        return 1 + number
    }
    return addOne
}
var increment = makeIncrementer()
increment(7)

//Idiomatic Scala

def makeIncrementer: Int => Int =
    (number: Int) => 1 + number

var increment = makeIncrementer
increment(7)
Map
Kotlin
val numbers = listOf(20, 19, 7, 12)
numbers.map { 3 * it }
Scala
var numbers = Array(20, 19, 7, 12)
numbers.map( number => 3 * number )
Sort
Kotlin
listOf(1, 5, 3, 12, 2).sorted()
Scala
Array(1, 5, 3, 12, 2).sortWith(_ > _)

Named Arguments
Kotlin
fun area(width: Int, height: Int) = width * height
area(width = 2, height = 3)

// This is also possible with named arguments
area(2, height = 2)
area(height = 3, width = 2)
Scala
def area(width: Int, height: Int): Int = {
    return width * height
}

area(width = 2, height = 3)
CLASSES
Declaration
Kotlin
class Shape {
    var numberOfSides = 0
    fun simpleDescription() =
        "A shape with $numberOfSides sides."
}
Scala
class Shape {
    var numberOfSides = 0
    def simpleDescription(): String = {
        return s"A shape with $numberOfSides sides."
    }
}
Usage
Kotlin
var shape = Shape()
shape.numberOfSides = 7
var shapeDescription = shape.simpleDescription()
Scala
var shape = new Shape()
shape.numberOfSides = 7
var shapeDescription = shape.simpleDescription()
Subclass
Kotlin
open class NamedShape(val name: String) {
    var numberOfSides = 0

    open fun simpleDescription() =
        "A shape with $numberOfSides sides."
}

class Square(var sideLength: BigDecimal, name: String) :
        NamedShape(name) {
    init {
        numberOfSides = 4
    }

    fun area() = sideLength.pow(2)

    override fun simpleDescription() =
        "A square with sides of length $sideLength."
}

val test = Square(BigDecimal("5.2"), "square")
test.area()
test.simpleDescription()
Scala
class NamedShape(var name: String,
                 var numberOfSides: Int = 0) {
    def simpleDescription =
        s"A shape with $numberOfSides sides."
}

class Square(var sideLength: Double, name: String)
    extends NamedShape(name, numberOfSides = 4) {
    def area = sideLength * sideLength
    override def simpleDescription =
        s"A square with sides of length $sideLength."
}

val test = new Square(5.2, "my test square")
test.area
test.simpleDescription
Checking Type
Kotlin
var movieCount = 0
var songCount = 0

for (item in library) {
    if (item is Movie) {
        ++movieCount
    } else if (item is Song) {
        ++songCount
    }
}
Scala
var movieCount = 0
var songCount = 0

for (item <- library) {
    if (item.isInstanceOf[Movie]) {
        movieCount += 1
    } else if (item.isInstanceOf[Song]) {
        songCount += 1
    }
}
Downcasting
Kotlin
for (current in someObjects) {
    if (current is Movie) {
        println("Movie: '${current.name}', " +
	    "dir. ${current.director}")
    }
}
Scala
for (obj <- someObjects) {
    val movie = obj.asInstanceOf[Movie]
    println(s"Movie: '${movie.name}', dir. ${movie.director}")
}
Protocol
Kotlin
interface Nameable {
    fun name(): String
}

fun f(x: T) {
    println("Name is " + x.name())
}
Scala
trait Nameable {
    def name(): String
}

def f[T <: Nameable](x: T) = {
    println("Name is " + x.name())
}

Extensions
Kotlin
val Double.km: Double get() = this * 1000
val Double.m: Double get() = this
val Double.cm: Double get() = this / 100
val Double.mm: Double get() = this / 1000
val Double.ft: Double get() = this / 3.28084

val oneInch = 25.4.mm
println("One inch is $oneInch meters")
// prints "One inch is 0.0254 meters"
val threeFeet = 3.0.ft
println("Three feet is $threeFeet meters")
// prints "Three feet is 0.914399970739201 meters"
Scala
object Extensions {
    implicit class DoubleUnit(d: Double) {
        def km: Double = { return d * 1000.0 }
        def m: Double = { return d }
        def cm: Double = { return d / 100.0 }
        def mm: Double = { return d / 1000.0 }
        def ft: Double = { return d / 3.28084 }
    }
}

import Extensions.DoubleUnit

val oneInch = 25.4.mm
println(s"One inch is $oneInch meters")
// prints "One inch is 0.0254 meters"
val threeFeet = 3.ft
println(s"Three feet is $threeFeet meters")
// prints "Three feet is 0.914399970739201 meters"