Language feature
|
Kotlin
|
Scala
|
Function
|
fun triple(x: Int): Int {
return x * 3
}
|
def triple(x: Int): Int = {
x * 3
}
|
Single statement function
|
fun triple(x: Int) = x * 3
|
def triple(x: Int) = x * 3
|
Immutable data class
|
data class Name(
val first: String,
val last: String
)
|
case class Name(
first: String,
last: String
)
|
Mutable data class
|
data class Name(
var first: String,
var last: String
)
|
case class Name(
var first: String,
var last: String
)
|
Enum
|
enum class Day(val isFun: Boolean) {
Monday(false),
Tuesday(false),
Wednesday(false),
Thursday(false),
Friday(false),
Saturday(true),
Sunday(true),
}
|
enum Day(val isFun: Boolean):
case Monday extends Day(false)
case Tuesday extends Day(false)
case Wednesday extends Day(false)
case Thursday extends Day(false)
case Friday extends Day(false)
case Saturday extends Day(true)
case Sunday extends Day(true)
|
Nullable (kotlin)/Option (scala)
|
data class Name(
val first: String,
val middle: String?,
val last: String
)
val shortName = Name(
first = "John",
middle = null,
last = "Doe"
)
val fullName = Name(
first = "Jane",
middle = "Lynn",
last = "Doe"
)
|
case class Name(
val first: String,
val middle: Option[String],
val last: String
)
val shortName = Name(
first = "John",
middle = Option.empty,
last = "Doe"
)
val fullName = Name(
first = "Jane",
middle = Option("Lynn"),
last = "Doe"
)
|
Default parameter values
|
data class Name(
val first: String,
val middle: String? = null,
val last: String
)
val shortName = Name(
first = "John",
last = "Doe"
)
val fullName = Name(
first = "Jane",
middle = "Lynn",
last = "Doe"
)
|
case class Name(
val first: String,
val middle: Option[String] = Option.empty,
val last: String
)
val shortName = Name(
first = "John",
last = "Doe"
)
val fullName = Name(
first = "Jane",
middle = Option("Lynn"),
last = "Doe"
)
|
Lambda
|
fun tripleList(x: List<Int>) =
x.map { item -> item * 3 }
fun tripleListImplicitParameter(x: List<Int>) =
x.map { it * 3 }
|
def tripleList(x: List[Int]) =
x.map(item => item * 3)
def tripleListImplicitParameter(x: List[Int]) =
x.map(_ * 3)
|
when/match
|
fun isSchoolNight(day: Day) = when (day) {
Day.Sunday,
Day.Monday,
Day.Tuesday,
Day.Wednesday,
Day.Thursday -> true
else -> false
}
|
def isSchoolNight(day: Day) = day match {
case Day.Sunday |
Day.Monday |
Day.Tuesday |
Day.Wednesday |
Day.Thursday => true
case _ => false
}
|
lazy val
|
val foo: Foo by lazy {
loadFoo()
}
|
lazy val foo: Foo = {
loadFoo()
}
|
Extension function
|
fun Int.triple() = this * 3
|
extension (x: Int) def triple() = {
x * 3
}
|
Companion object
|
class Foo {
fun greet() = println(MESSAGE)
companion object {
private const val MESSAGE = "hello"
}
}
|
class Foo {
def greet() = println(Foo.MESSAGE)
}
object Foo {
private final val MESSAGE = "hello"
}
|
Interfaces
|
interface Greeter {
fun greet()
}
class ConsoleGreeter() : Greeter {
override fun greet() = println("Hello")
}
|
trait Greeter {
def greet(): Unit
}
class ConsoleGreeter extends Greeter {
override def greet() = println("Hello")
}
|
Range iteration
|
val intRangeTripled: List<Int> =
(0 until 5).map { it * 3 }
|
val intRangeTripled: List[Int] =
(0 until 5).toList.map(_ * 3)
|
String template
|
val displayName = "${name.first} ${name.last}"
|
val displayName = s"${name.first} ${name.last}"
|