Kotlin is a better language for the JVM than Java
Really easy to get up and running
Simpler language than Java
Much safer than Java
No more NullPointerExceptions!
Streamlines the handling of null values
More readable than Java
Has an excellent standard library
100% Interoperable with Java
"Coroutines are always related to some local scope in your application, which is an entity with a limited life-time, like a UI element"
private val parentJob = Job()
private val uiScope = CoroutineScope(Dispatchers.Main + parentJob)
fun requestSomeData() {
uiScope.launch {
updateUI(performRequest())
}
}
override fun onDestroy() {
parentJob.cancel()
}fun requestSomeData() {
launch {
updateUI(performRequest())
}
}
val result = withContext(Dispatchers.IO) {
queryDatabase()
}
withContext(Dispatchers.Main) {
updateUI(result)
}sealed class Expr
data class Const(val number: Double) : Expr()
data class Sum(val e1: Expr, val e2: Expr) : Expr()
object NotANumber : Expr()fun eval(expr: Expr): Double = when(expr) {
is Const -> expr.number
is Sum -> eval(expr.e1) + eval(expr.e2)
NotANumber -> Double.NaN
// the `else` clause is not required
// because we've covered all the cases
}Nested Test classes
@TestInstance(PER_CLASS)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class DesignControllerTest {
private val dao: DesignDAO = mockk()
private val mapper: DesignMapper = mockk()
private val controller = DesignController(dao, mapper)
@BeforeEach
fun init() {
clearMocks(dao, mapper)
}
// takes 250 ms
@RepeatedTest(300)
fun foo() {
controller.doSomething()
}
}inline fun <reified T> fromHashTable(
hashTable: Hashtable<String, out Any>): T {
return objectMapper.readValue(
objectMapper.writeValueAsString(hashTable),
T::class.java)
}I'm a huge fan and advocate of the functional programming style over the imperative style.
Arrow