import { Box } from "./box" import { DefaultOrFunc, getDefault } from "./fn" import { Voidable } from "./maybe" /** Optional values * @deprecated */ export class Option implements Box> { val: Voidable #has: boolean private constructor(has: boolean, val?: T) { this.#has = has if (has) this.val = val } get has() { return this.#has } toString() { if (this.#has) return `Some(${this.val})` else return `None` } map(f: (val: T) => R): Option { if (this.#has) return Option.some(f(this.val!)) return Option.none() } then(f: (val: T) => Option): Option { if (this.#has) return f(this.val!) return Option.none() } or(other: DefaultOrFunc>): Option { if (this.#has) return this else return getDefault(other) } xor(other: Option): Option { if (this.#has && !other.has) return this else if (!this.#has && other.has) return other else return Option.none() } and(other: Option): Option { if (this.#has) return other else return Option.none() } take(): Option { if (this.#has) { this.#has = false return Option.some(this.val!) } return Option.none() } static some(value: T) { return new Option(true, value) } static none(): Option { return Option.None } static None = new Option(false) }