Home >Web Front-end >JS Tutorial >Exploring Option Getters in Effect-TS

Exploring Option Getters in Effect-TS

王林
王林Original
2024-07-18 00:37:32683browse

Exploring Option Getters in Effect-TS

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.

Example 1: Using O.getOrElse

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)
}

Example 2: Using O.getOrThrow

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
  }
}

Example 3: Using O.getOrNull

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)
}

Example 4: Using O.getOrUndefined

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)
}

Example 5: Using O.getOrThrowWith

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
  }
}

Conclusion

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn