or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

collection-extensions.mderror-handling.mdindex.mdoptional-values.mdpartial-results.mdproduct-types.mdraise-dsl.mdsafe-collections.mdutility-functions.md

optional-values.mddocs/

0

# Optional Values

1

2

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.

3

4

## Core Types

5

6

```kotlin { .api }

7

sealed class Option<out A>

8

9

data class Some<out T>(val value: T) : Option<T>()

10

11

object None : Option<Nothing>

12

```

13

14

## Construction

15

16

### From Values

17

18

```kotlin { .api }

19

// Create Some

20

fun <A> A.some(): Option<A>

21

22

// Create None

23

fun <A> none(): Option<A>

24

25

// From nullable

26

fun <A> A?.toOption(): Option<A>

27

```

28

29

### Static Constructors

30

31

```kotlin { .api }

32

companion object Option {

33

fun <A> fromNullable(a: A?): Option<A>

34

fun <A> invoke(a: A): Option<A>

35

36

// Safe exception handling

37

fun <A> catch(f: SingletonRaise<None>.() -> A): Option<A>

38

fun <A> catch(recover: (Throwable) -> Option<A>, f: SingletonRaise<None>.() -> A): Option<A>

39

}

40

```

41

42

## Inspection

43

44

```kotlin { .api }

45

// Type checking

46

fun isNone(): Boolean

47

fun isSome(): Boolean

48

fun isSome(predicate: (A) -> Boolean): Boolean

49

50

// Value extraction

51

fun getOrNull(): A?

52

fun getOrElse(default: () -> A): A

53

```

54

55

## Transformation

56

57

```kotlin { .api }

58

// Map operations

59

fun <B> map(f: (A) -> B): Option<B>

60

fun <B> flatMap(f: (A) -> Option<B>): Option<B>

61

62

// Filtering

63

fun filter(predicate: (A) -> Boolean): Option<A>

64

fun filterNot(predicate: (A) -> Boolean): Option<A>

65

66

// Type filtering

67

inline fun <reified B> filterIsInstance(): Option<B>

68

```

69

70

## Pattern Matching

71

72

```kotlin { .api }

73

// Fold operation

74

fun <R> fold(ifEmpty: () -> R, ifSome: (A) -> R): R

75

```

76

77

## Side Effects

78

79

```kotlin { .api }

80

// Execute side effects

81

fun onNone(action: () -> Unit): Option<A>

82

fun onSome(action: (A) -> Unit): Option<A>

83

```

84

85

## Conversion

86

87

```kotlin { .api }

88

// Convert to other types

89

fun <L> toEither(ifEmpty: () -> L): Either<L, A>

90

fun toList(): List<A>

91

```

92

93

## Combining Options

94

95

```kotlin { .api }

96

// Combine two Options

97

fun combine(other: Option<A>, combine: (A, A) -> A): Option<A>

98

99

// Flatten nested Options

100

fun Option<Option<A>>.flatten(): Option<A>

101

```

102

103

## Error Recovery

104

105

```kotlin { .api }

106

// Recover from None using Raise DSL

107

fun recover(recover: SingletonRaise<None>.() -> A): Option<A>

108

```

109

110

## Comparison

111

112

```kotlin { .api }

113

// Compare Options (when A is Comparable)

114

operator fun <A : Comparable<A>> compareTo(other: Option<A>): Int

115

```

116

117

## Interoperability

118

119

```kotlin { .api }

120

// Convert from Map operations

121

fun <K, V> Option<Pair<K, V>>.toMap(): Map<K, V>

122

123

// With Effect types

124

suspend fun <A> Effect<None, A>.toOption(): Option<A>

125

fun <A> EagerEffect<None, A>.toOption(): Option<A>

126

```

127

128

## Usage Examples

129

130

### Basic Operations

131

132

```kotlin

133

import arrow.core.*

134

135

// Create Options

136

val someValue = "Hello".some()

137

val noneValue = none<String>()

138

139

// Transform values

140

val length = someValue.map { it.length } // Some(5)

141

val empty = noneValue.map { it.length } // None

142

143

// Extract values

144

val result1 = someValue.getOrElse { "Default" } // "Hello"

145

val result2 = noneValue.getOrElse { "Default" } // "Default"

146

```

147

148

### Chaining Operations

149

150

```kotlin

151

import arrow.core.*

152

153

data class User(val name: String, val email: String?)

154

155

fun findUser(id: Int): Option<User> =

156

if (id > 0) User("Alice", "alice@example.com").some()

157

else none()

158

159

fun validateEmail(email: String): Option<String> =

160

if (email.contains('@')) email.some() else none()

161

162

val userEmail = findUser(1)

163

.flatMap { user -> user.email.toOption() }

164

.flatMap { email -> validateEmail(email) }

165

.getOrElse { "No valid email" }

166

```

167

168

### Error Handling

169

170

```kotlin

171

import arrow.core.*

172

173

// Safe division

174

fun safeDivide(x: Int, y: Int): Option<Int> =

175

Option.catch { x / y }

176

177

val result = safeDivide(10, 0) // None (catches ArithmeticException)

178

val success = safeDivide(10, 2) // Some(5)

179

```

180

181

### Integration with Nullable Types

182

183

```kotlin

184

import arrow.core.*

185

186

// Convert nullable to Option

187

val nullable: String? = null

188

val option = nullable.toOption() // None

189

190

// Work with Optional chains

191

fun processUser(name: String?): String =

192

name.toOption()

193

.filter { it.isNotBlank() }

194

.map { "Hello, $it!" }

195

.getOrElse { "Invalid name" }

196

```