Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion stdlib/option.affine
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
//
// Option - Utilities for Option<T> type

// Option type is defined in prelude, but here are utilities
module option;

// `Option`/`Result` types + constructors are owned by `prelude` (ADR-011).
use prelude::{Option, Some, None, Result, Ok, Err};

// This module is the single canonical home for the Option *operations*
// (is_some/is_none/unwrap/unwrap_or/map/filter/contains/…). #133 removed
// the duplicate copies that previously also lived in prelude.affine.

// ============================================================================
// Combinators
Expand Down
72 changes: 11 additions & 61 deletions stdlib/prelude.affine
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,24 @@
// AffineScript Standard Library - Prelude
// Common functions and utilities automatically available

module prelude;

// ============================================================================
// Option type - represents optional values
// Core sum types (canonical home — ADR-011)
//
// `Option` and `Result` (and their constructors) are owned here, the
// foundational module. The `option` / `result` modules provide the
// *operations* over them and `use prelude::{...}` for the types. The
// duplicate/conflicting Option/Result ops that previously lived here
// (is_some, is_none, unwrap, unwrap_or, is_ok, is_err, unwrap_result,
// unwrap_or_result) were removed in #133: option::* and result::* are
// the single canonical bindings for those.
// ============================================================================

type Option<T> = Some(T) | None

fn is_some<T>(opt: Option<T>) -> Bool {
match opt {
Some(_) => true,
None => false
}
}

fn is_none<T>(opt: Option<T>) -> Bool {
match opt {
Some(_) => false,
None => true
}
}

fn unwrap<T>(opt: Option<T>) -> T {
match opt {
Some(value) => value,
None => panic("Called unwrap on None")
}
}

fn unwrap_or<T>(opt: Option<T>, default: T) -> T {
match opt {
Some(value) => value,
None => default
}
}

// ============================================================================
// Result type - represents success or failure
// ============================================================================

type Result<T, E> = Ok(T) | Err(E)

fn is_ok<T, E>(res: Result<T, E>) -> Bool {
match res {
Ok(_) => true,
Err(_) => false
}
}

fn is_err<T, E>(res: Result<T, E>) -> Bool {
match res {
Ok(_) => false,
Err(_) => true
}
}

fn unwrap_result<T, E>(res: Result<T, E>) -> T {
match res {
Ok(value) => value,
Err(_) => panic("Called unwrap on Err")
}
}

fn unwrap_or_result<T, E>(res: Result<T, E>, default: T) -> T {
match res {
Ok(value) => value,
Err(_) => default
}
}

// ============================================================================
// List utilities
// ============================================================================
Expand Down
10 changes: 9 additions & 1 deletion stdlib/result.affine
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@
//
// Result - Error handling with Result<T, E> type

// Result type is defined in prelude, but here are utilities
module result;

// `Option`/`Result` types + constructors are owned by `prelude` (ADR-011).
use prelude::{Result, Ok, Err, Option, Some, None};

// This module is the single canonical home for the Result *operations*
// (is_ok/is_err/unwrap/unwrap_or/map_ok/map_err/…). #133 removed the
// duplicate/redundant copies (is_ok/is_err/unwrap_result/unwrap_or_result)
// that previously also lived in prelude.affine.

// ============================================================================
// Constructors and Conversions
Expand Down