Singapore Scala Meetup - 25 April 2018
(for Scala programmers)
_hhandoko
hhandoko
hhandoko.com
Basically...
// Immutable (with type annotations)
val greeting: String = "Hello"
// Mutable (with type inference)
var world = "World!"
world = "Singapore!"
// Scala
final case class User(
name: String,
age: Int
)
// Kotlin
data class User(
val name: String,
val age: Int
)
// Scala
val name = "Herdy"
val greeting = s"Hello $name!"
val multiline =
"""An ocean voyage.
|As waves break over the bow,
|the sea welcomes $name.
""".trimMargin
// Kotlin
val name = "Herdy"
val greeting = "Hello $name!"
val multiline =
"""An ocean voyage.
|As waves break over the bow,
|the sea welcomes $name.
""".stripMargin
// Scala
lazy val pi: Double =
22 / 7
// Kotlin
val pi: Double by lazy {
22 / 7
}
// Scala
class User {
def run: Unit = ???
}
object User {
final val STAMINA = 100
}
// Kotlin
class User() {
fun run: Unit = TODO()
companion object {
const val STAMINA = 100
}
}
// Scala
val (first, last) =
("Herdy", "Handoko")
// Kotlin
val (first, last) =
Pair("Herdy", "Handoko")
// Scala
type Amount = BigDecimal
// Kotlin
typealias Amount = BigDecimal
// Scala
val dateOpt: Option[LocalDate] = None
val day = dateOpt.map { date =>
date.getDayOfMonth
}
val today = LocalDate.now
val todayOpt = today // ERROR!
// Kotlin
val dateOpt: LocalDate? = null
val day = dateOpt?.let { date ->
date.dayOfMonth
}
val today = LocalDate.now
val todayOpt = today // OK!
// Scala
implicit class RSHelper(rs: ResultSet) {
def parseString(f: String): String =
rs.getString(f)
}
// Kotlin
fun ResultSet.parseString(f: String) =
this.getString(f)
// Scala
val someFunction = {
???
}
// Kotlin
val someFunction = let {
TODO()
}
// Scala
sealed trait Car
case class BMW(series: String)
extends Car
case object MercedesBenz
extends Car
// Kotlin
sealed class Car
data class BMW(val series: String)
: Car()
object MercedesBenz
: Car()
// Scala
phones.map { phone => phone.model }
phones.map(_.model)
phones.filter(_.model == "Nokia")
phones.head
books.map { case (_, title) =>
title.toUpperCase
}
// Kotlin
phones.map { phone -> phone.model }
phones.map { it.model }
phones.filter { it.model == "Nokia" }
phones.first()
books.map { (_, title) ->
title.toUpperCase
}
// Scala
car match {
case BMW(s) => println(s"BMW Series $s")
case MercedesBenz => println("Mercedes-Benz")
}
// Kotlin
when (car) {
is BMW -> println("BMW Series ${car.series}")
is MercedesBenz -> println("Mercedes-Benz")
}
// Kotlin - with `noarg` plugin
data class User(
var name: String
var country: String
)
// Java
User user = new User();
user.setName("Herdy");
user.setCountry("Singapore");
// Kotlin
data class User @JvmOverloads(
val name: String,
val age: Int = 21
)
// Kotlin
class User {
companion object {
@JvmStatic
val stamina =
BigDecimal(100)
@JvmStatic
fun run(): Unit =
TODO()
}
}
// Kotlin
val user = User()
.apply {
this.name = "Herdy"
this.age = 21
}.run {
save(this)
}.also {
logger.debug("Persisted!")
}
// Kotlin
val name: String? =
UserDAO.get(id)
?.let { it.name }
?: throw IllegalArgumentException()