Skip to content

q-position ​

q(...) is used where it can't be rewritten into an early return.

Why: q(result) becomes "if this is an Err, return it; otherwise continue with the value". That is only a plain rewrite when q(...) is the whole statement: const x = q(..), x = q(..), q(..); or return q(..). Inside a larger expression, the order of evaluation would change. Guide: Errors.

✕ Rejectedq-position
ts
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, q, type Result } from "@evobug_com/safe-ts-std/result";

type PriceError = Enum<{ NoItem: {} }>;
const PriceError = constructors<PriceError>();

function price(item: string): Result<number, PriceError> {
  return item === "" ? err(PriceError.NoItem()) : 10;
}

export function total(a: string, b: string): Result<number, PriceError> {
  return q(price(a)) + q(price(b));
}
✓ Accepted0 diagnostics
ts
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, q, type Result } from "@evobug_com/safe-ts-std/result";

type PriceError = Enum<{ NoItem: {} }>;
const PriceError = constructors<PriceError>();

function price(item: string): Result<number, PriceError> {
  return item === "" ? err(PriceError.NoItem()) : 10;
}

export function total(a: string, b: string): Result<number, PriceError> {
  const first = q(price(a));
  const second = q(price(b));
  return first + second;
}
ts
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { Err as __qErr } from "@evobug_com/safe-ts-std/result"; import { err, q, type Result } from "@evobug_com/safe-ts-std/result";

type PriceError = Enum<{ NoItem: {} }>;
const PriceError = constructors<PriceError>();

function price(item: string): Result<number, PriceError> {
  return item === "" ? err(({ tag: "NoItem" } satisfies PriceError as PriceError)) : 10;
}

export function total(a: string, b: string): Result<number, PriceError> {
  const __q1 = price(a); if (__q1 instanceof __qErr) return __q1; const first = __q1;
  const __q2 = price(b); if (__q2 instanceof __qErr) return __q2; const second = __q2;
  return first + second;
}

Also rejected: q in an arrow's expression body (items.map((i) => q(parse(i)))), in a declaration with several variables, and in a for (...) head.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra