Skip to content

Commit 63ade78

Browse files
fix(stdlib): single-ownership dedup of prelude/option/result (#133) (#152)
Per ADR-011 (#132): one canonical binding per name, module-owned. - prelude.affine: `module prelude;`. Keeps the core sum types `Option`/`Result` (+ constructors) as their canonical home and the generic list/numeric utilities. Removes the 8 duplicate/conflicting Option/Result *operations* it previously also defined (is_some, is_none, unwrap, unwrap_or, is_ok, is_err, unwrap_result, unwrap_or_result). - option.affine: `module option;` + `use prelude::{Option, Some, None, Result, Ok, Err};`. Now the single canonical home for Option ops. - result.affine: `module result;` + `use prelude::{Result, Ok, Err, Option, Some, None};`. Single canonical home for Result ops. The previous flat-namespace conflicts (`prelude.map(arr,f)` vs `option.map(f,opt)`; `prelude.unwrap`(Option) vs `result.unwrap`(Result)) are resolved by module ownership — each is now `module::name`, exactly one definition per owning module. Verified: no leftover dup defs, no prelude util calls a removed op, full suite green (214 tests). Note: this removes the prelude `unwrap`/`unwrap_result` that #134 (PR #150) patched; the sound, panicking versions are `option::unwrap` and `result::unwrap` (already correct). #150's regression tests remain valid against those. Merge-order: if #150 lands first, resolve the modify/delete in prelude.affine in favour of this deletion. Closes #133 Refs #128, #132 Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent f879862 commit 63ade78

3 files changed

Lines changed: 28 additions & 63 deletions

File tree

stdlib/option.affine

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33
//
44
// Option - Utilities for Option<T> type
55

6-
// Option type is defined in prelude, but here are utilities
6+
module option;
7+
8+
// `Option`/`Result` types + constructors are owned by `prelude` (ADR-011).
9+
use prelude::{Option, Some, None, Result, Ok, Err};
10+
11+
// This module is the single canonical home for the Option *operations*
12+
// (is_some/is_none/unwrap/unwrap_or/map/filter/contains/…). #133 removed
13+
// the duplicate copies that previously also lived in prelude.affine.
714

815
// ============================================================================
916
// Combinators

stdlib/prelude.affine

Lines changed: 11 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,24 @@
22
// AffineScript Standard Library - Prelude
33
// Common functions and utilities automatically available
44

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

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

11-
fn is_some<T>(opt: Option<T>) -> Bool {
12-
match opt {
13-
Some(_) => true,
14-
None => false
15-
}
16-
}
17-
18-
fn is_none<T>(opt: Option<T>) -> Bool {
19-
match opt {
20-
Some(_) => false,
21-
None => true
22-
}
23-
}
24-
25-
fn unwrap<T>(opt: Option<T>) -> T {
26-
match opt {
27-
Some(value) => value,
28-
None => panic("Called unwrap on None")
29-
}
30-
}
31-
32-
fn unwrap_or<T>(opt: Option<T>, default: T) -> T {
33-
match opt {
34-
Some(value) => value,
35-
None => default
36-
}
37-
}
38-
39-
// ============================================================================
40-
// Result type - represents success or failure
41-
// ============================================================================
42-
4321
type Result<T, E> = Ok(T) | Err(E)
4422

45-
fn is_ok<T, E>(res: Result<T, E>) -> Bool {
46-
match res {
47-
Ok(_) => true,
48-
Err(_) => false
49-
}
50-
}
51-
52-
fn is_err<T, E>(res: Result<T, E>) -> Bool {
53-
match res {
54-
Ok(_) => false,
55-
Err(_) => true
56-
}
57-
}
58-
59-
fn unwrap_result<T, E>(res: Result<T, E>) -> T {
60-
match res {
61-
Ok(value) => value,
62-
Err(_) => panic("Called unwrap on Err")
63-
}
64-
}
65-
66-
fn unwrap_or_result<T, E>(res: Result<T, E>, default: T) -> T {
67-
match res {
68-
Ok(value) => value,
69-
Err(_) => default
70-
}
71-
}
72-
7323
// ============================================================================
7424
// List utilities
7525
// ============================================================================

stdlib/result.affine

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33
//
44
// Result - Error handling with Result<T, E> type
55

6-
// Result type is defined in prelude, but here are utilities
6+
module result;
7+
8+
// `Option`/`Result` types + constructors are owned by `prelude` (ADR-011).
9+
use prelude::{Result, Ok, Err, Option, Some, None};
10+
11+
// This module is the single canonical home for the Result *operations*
12+
// (is_ok/is_err/unwrap/unwrap_or/map_ok/map_err/…). #133 removed the
13+
// duplicate/redundant copies (is_ok/is_err/unwrap_result/unwrap_or_result)
14+
// that previously also lived in prelude.affine.
715

816
// ============================================================================
917
// Constructors and Conversions

0 commit comments

Comments
 (0)