Project Setup
Gradle
Kotlin
implementation("it.czerwinski:kotlin-util:2.0.0")
Groovy
implementation 'it.czerwinski:kotlin-util:2.0.0'
Maven
<dependency>
<groupId>it.czerwinski</groupId>
<artifactId>kotlin-util</artifactId>
<version>2.0.0</version>
</dependency>
Kotlin Multiplatform Projects
In multiplatform projects, the library can be used as commonMain dependency.
See also Kotlin Multiplatform Mobile Example.
Java Modules (JPMS)
In projects using Java Modules (JPMS), add the following line
to your module-info.java:
module your.module {
requires it.czerwinski.kotlin.util;
}
Supported Types
Utilities
Option
Implementation differences:
existshas been replaced withany– Kotlin conventionforallhas been replaced withall– Kotlin conventionforeachhas been replaced withforEach– Kotlin conventionorNullhas been replaced withgetOrNull– Kotlin conventionunzip3has been replaced withunzip– there is no ambiguity in Kotlin- implemented additional functions:
filterNotNull,filterIsInstance– Kotlin convention
Kotlin introduces its own null-safety mechanisms.
Most of the time, Options can be replaced by nullable types in Kotlin, e.g.:
var number: Option<Int> = // …
number.map { it.toString() }.getOrElse { "" }
gives the same result as:
var number: Int? = // …
number?.toString().orEmpty()
However, Options might be useful whenever null values are not allowed,
e.g. RxJava 2.x Observable.
Either
Implementation differences:
existshas been replaced withany– Kotlin conventionforallhas been replaced withall– Kotlin conventionforeachhas been replaced withforEach– Kotlin convention- implemented additional functions:
filterNot,filterNotNull,filterIsInstance– Kotlin convention - implemented additional functions:
filterToOption,filterNotToOption,filterNotNullToOption,filterIsInstanceToOption - implemented additional function
getOrNullin addition totoOption– Kotlin uses its own null-safety mechanisms
Since version 1.3, this implementation is right-biased, therefore it is possible
to use methods directly on Either instead of RightProjection,
e.g. it is possible to use Either.map {} instead of Either.right.map {}.
Use of RightProjection has been deprecated.
Scala convention that “dictates that Left is used for failure and Right is used for success”
(Scala Standard Library Documentation) is still a preferred way of using this class.
Try
Implementation differences:
foreachhas been replaced withforEach– Kotlin convention- implemented additional functions:
filterNot,filterNotNull,filterIsInstance– Kotlin convention - implemented additional function
getOrNullin addition totoOption– Kotlin uses its own null-safety mechanisms
Additional Iterators
EmptyIterator
Iterator based on the result of scala.collection.Iterator.empty, producing no values.
Kotlin defines an internal EmptyIterator, which can only be obtained indirectly
from empty collections, e.g.:
emptyList<Nothing>().iterator()
SingletonIterator
Iterator based on the result of scala.collection.Iterator.single[A](elem: A), producing a single value.
An iterator for a singleton list is defined in Java, in a package-private static method
java.util.Collections.singletonIterator(E e). It can only be obtained indirectly
from a new instance of a singleton list, e.g.:
java.util.Collections.singletonList(element).iterator()
Kotlin Multiplatform Mobile Example
- Create a new Kotlin Multiplatform Mobile project.
- In
shared/build.gradle.kts, addkotlin-utildependency tocommonMain:repositories { // [...] mavenCentral() } kotlin { // [...] sourceSets { // [...] val commonMain by getting { dependencies { implementation("it.czerwinski:kotlin-util:2.0.0") } } } } - Edit
Platformclass to returnOption<String>:commonMain:expect class Platform() { val platform: Option<String> }androidMain:actual class Platform actual constructor() { actual val platform: Option<String> = Some("Android ${android.os.Build.VERSION.SDK_INT}") }iosMain:actual class Platform actual constructor() { actual val platform: Option<String> = Some( UIDevice.currentDevice.systemName() + " " + UIDevice.currentDevice.systemVersion ) } - Edit
Greetingsclass to get value fromOption:class Greeting { fun greeting(): String { return "Hello, kotlin-util on ${Platform().platform.getOrElse { "unknown platform" }}!" } } - Run both mobile apps.