Kotlin Snippets

Checks / Exit Conditions Instead of writing something like if !condition throw …​ we may use require and check for quick exit conditions: val num = 1; require(num > 1){ // ... } val num = 1; require(num > 1){ // .. } Pluralize Strings Adding pluralization as extension function fun String.pluralize(count:Int):String { return if (count > 1){ this + 's' } else { this } } Use it like this ...

March 1, 2010 · 1 min · 160 words · Micha Kops