Effect-TS provides a robust set of tools for handling Option types, which represent values that may or may not exist. In this article, we'll explore various ways to get the value inside an Option using different getters provided by the library.
The O.getOrElse function allows you to provide a default value if the Option is None. This is useful when you want to ensure a fallback value is always available.
import { Option as O, pipe } from 'effect'; function getters_ex01() { const some = O.some(1); // Create an Option containing the value 1 const none = O.none(); // Create an Option representing no value console.log(pipe(some, O.getOrElse(() => 'none'))); // Output: 1 (since some contains 1) console.log(pipe(none, O.getOrElse(() => 'none'))); // Output: 'none' (since none is None) }
The O.getOrThrow function returns the value inside the Option if it is Some, otherwise it throws a default error.
import { Option as O, pipe } from 'effect'; function getters_ex02() { const some = O.some(1); // Create an Option containing the value 1 const none = O.none(); // Create an Option representing no value console.log(pipe(some, O.getOrThrow)); // Output: 1 (since some contains 1) try { console.log(pipe(none, O.getOrThrow)); // This will throw an error } catch (e) { console.log(e.message); // Output: getOrThrow called on a None } }
The O.getOrNull function returns the value inside the Option if it is Some, otherwise it returns null.
import { Option as O, pipe } from 'effect'; function getters_ex03() { const some = O.some(1); // Create an Option containing the value 1 const none = O.none(); // Create an Option representing no value console.log(pipe(some, O.getOrNull)); // Output: 1 (since some contains 1) console.log(pipe(none, O.getOrNull)); // Output: null (since none is None) }
The O.getOrUndefined function returns the value inside the Option if it is Some, otherwise it returns undefined.
import { Option as O, pipe } from 'effect'; function getters_ex04() { const some = O.some(1); // Create an Option containing the value 1 const none = O.none(); // Create an Option representing no value console.log(pipe(some, O.getOrUndefined)); // Output: 1 (since some contains 1) console.log(pipe(none, O.getOrUndefined)); // Output: undefined (since none is None) }
import { Option as O, pipe } from 'effect'; function getters_ex05() { const some = O.some(1); // Create an Option containing the value 1 const none = O.none(); // Create an Option representing no value console.log(pipe(some, O.getOrThrowWith(() => new Error('Custom Error')))); // Output: 1 (since some contains 1) try { console.log(pipe(none, O.getOrThrowWith(() => new Error('Custom Error')))); // This will throw a custom error } catch (e) { console.log(e.message); // Output: Custom Error } }
By using these different getters, you can effectively handle Option types in various scenarios, ensuring your code behaves correctly whether an Option is Some or None. These utilities provide a clear, type-safe way to work with optional values, avoiding common pitfalls associated with null checks and enhancing the readability and maintainability of your code. Adopting these patterns can lead to cleaner, more robust codebases where
The above is the detailed content of Exploring Option Getters in Effect-TS. For more information, please follow other related articles on the PHP Chinese website!