How to idiomatically test for non-null, non-empty strings in Kotlin?

You can use isNullOrEmpty or its friend isNullOrBlank like so: if(!s.isNullOrEmpty()){ // s is not empty } Both isNullOrEmpty and isNullOrBlank are extension methods on CharSequence? thus you can use them safely with null. Alternatively turn null into false like so: if(s?.isNotEmpty() ?: false){ // s is not empty } you can also do the … Read more

What is the point of tailrec in Kotlin?

The keyword tells the compiler that the implementation of the function is required to be tail-recursive, and causes the compiler to report an error if the function is not actually tail-recursive. It protects the user from situations when a change to the implementation of the function causes it to no longer be tail-recursive, and causes … Read more

How to check generic type in Kotlin?

If you need to check if something is of generic type T you need to to have an instance of Class<T> to check against. This is a common technique in Java however in Kotlin we can make use of an inlined factory method that gets us the class object. class Generic<T : Any>(val klass: Class<T>) … Read more