Functional companion to Kotlin's Standard Library providing type-safe error handling and functional programming constructs for iOS x64 target
—
The Option type provides type-safe handling of nullable values, eliminating null pointer exceptions through functional programming patterns. It represents values that may or may not be present.
sealed class Option<out A>
data class Some<out T>(val value: T) : Option<T>()
object None : Option<Nothing>// Create Some
fun <A> A.some(): Option<A>
// Create None
fun <A> none(): Option<A>
// From nullable
fun <A> A?.toOption(): Option<A>companion object Option {
fun <A> fromNullable(a: A?): Option<A>
fun <A> invoke(a: A): Option<A>
// Safe exception handling
fun <A> catch(f: SingletonRaise<None>.() -> A): Option<A>
fun <A> catch(recover: (Throwable) -> Option<A>, f: SingletonRaise<None>.() -> A): Option<A>
}// Type checking
fun isNone(): Boolean
fun isSome(): Boolean
fun isSome(predicate: (A) -> Boolean): Boolean
// Value extraction
fun getOrNull(): A?
fun getOrElse(default: () -> A): A// Map operations
fun <B> map(f: (A) -> B): Option<B>
fun <B> flatMap(f: (A) -> Option<B>): Option<B>
// Filtering
fun filter(predicate: (A) -> Boolean): Option<A>
fun filterNot(predicate: (A) -> Boolean): Option<A>
// Type filtering
inline fun <reified B> filterIsInstance(): Option<B>// Fold operation
fun <R> fold(ifEmpty: () -> R, ifSome: (A) -> R): R// Execute side effects
fun onNone(action: () -> Unit): Option<A>
fun onSome(action: (A) -> Unit): Option<A>// Convert to other types
fun <L> toEither(ifEmpty: () -> L): Either<L, A>
fun toList(): List<A>// Combine two Options
fun combine(other: Option<A>, combine: (A, A) -> A): Option<A>
// Flatten nested Options
fun Option<Option<A>>.flatten(): Option<A>// Recover from None using Raise DSL
fun recover(recover: SingletonRaise<None>.() -> A): Option<A>// Compare Options (when A is Comparable)
operator fun <A : Comparable<A>> compareTo(other: Option<A>): Int// Convert from Map operations
fun <K, V> Option<Pair<K, V>>.toMap(): Map<K, V>
// With Effect types
suspend fun <A> Effect<None, A>.toOption(): Option<A>
fun <A> EagerEffect<None, A>.toOption(): Option<A>import arrow.core.*
// Create Options
val someValue = "Hello".some()
val noneValue = none<String>()
// Transform values
val length = someValue.map { it.length } // Some(5)
val empty = noneValue.map { it.length } // None
// Extract values
val result1 = someValue.getOrElse { "Default" } // "Hello"
val result2 = noneValue.getOrElse { "Default" } // "Default"import arrow.core.*
data class User(val name: String, val email: String?)
fun findUser(id: Int): Option<User> =
if (id > 0) User("Alice", "alice@example.com").some()
else none()
fun validateEmail(email: String): Option<String> =
if (email.contains('@')) email.some() else none()
val userEmail = findUser(1)
.flatMap { user -> user.email.toOption() }
.flatMap { email -> validateEmail(email) }
.getOrElse { "No valid email" }import arrow.core.*
// Safe division
fun safeDivide(x: Int, y: Int): Option<Int> =
Option.catch { x / y }
val result = safeDivide(10, 0) // None (catches ArithmeticException)
val success = safeDivide(10, 2) // Some(5)import arrow.core.*
// Convert nullable to Option
val nullable: String? = null
val option = nullable.toOption() // None
// Work with Optional chains
fun processUser(name: String?): String =
name.toOption()
.filter { it.isNotBlank() }
.map { "Hello, $it!" }
.getOrElse { "Invalid name" }Install with Tessl CLI
npx tessl i tessl/maven-io-arrow-kt--arrow-core-iosx64