The Curious Case of ?.let {} ?: run {}

Try this code :

fun main() {
val theCase: Int? = 1
theCase?.let {
fun1()
fun2()
} ?: run {
funError()
}
}

fun fun1() {
println(
"fun1")
}

fun fun2(): Unit? {
// return println("fun2")
return null
}

fun funError(){
println(
"funError")
}

The result :

fun1
funError

So the funError() was called - but it shouldn’t?

Play with the code and change the fun2() to :

fun fun2(): Unit? {
return println("fun2")
// return null
}

Generally speaking, funError() should be called only when (?)

val theCase: Int? = null

Not really …

You can read more about it (and many others) in details here :


Git RERERE +.gitconfig for folder directory -> no more problems with git users for private and work profiles.


What is the problem with Kotlin .all {} extension function from Kotlin Collection?

Documentation :

/**
* Returns `true` if all elements match the given [predicate].
*
*
@sample samples.collections.Collections.Aggregates.all
*/
public inline fun <T> Iterable<T>.all(predicate: (T) -> Boolean): Boolean {
if (this is Collection && isEmpty()) return true
for (element in this) if (!predicate(element)) return false
return true
}

So if you have a collection … let’s say like this :

val list = listOf<String>("OK", "OK")

and you want to check if all elements match the given condition: “are all elements in collection equals “OK”?

You can try this…


Github repo : https://github.com/MateuszWolkowicz/CardsCarouselWithDragAndDropToDelete

  • Horizontal RecyclerView
  • Span selected item to centre
  • Highlight card while scrolling
  • Select only when scrolling is finished
  • Add new cards and smooth scroll to position
  • Drag and Drop to delete
  • Change background while dragging

Starting from Android Marshmallow (Android 6.0; API level 23) all Android devices takes care of battery optimization by default and force system to use as small connectivity, resources and CPU as possible to achieve it.

There are two power-saving features which reduce battery consumption and one of it is DOZE MODE.

What is DOZE MODE in details you can find here: https://developer.android.com/training/monitoring-device-state/doze-standby

If you are reading this article you probably know what it is and you are only looking for some simple answers for a few questions :

  1. “How to test DOZE MODE ?
  2. “How to set DOZE MODE…

Mateusz Wołkowicz

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store