Skip to content

Commit 557f00b

Browse files
hyperpolymathclaude
andcommitted
fix(stdlib): single-ownership dedup of prelude/option/result (#133)
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 dab2f39 commit 557f00b

3 files changed

Lines changed: 28 additions & 69 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 & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,80 +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 => {
29-
println("Called unwrap on None");
30-
// TODO: panic!() once implemented
31-
}
32-
}
33-
}
34-
35-
fn unwrap_or<T>(opt: Option<T>, default: T) -> T {
36-
match opt {
37-
Some(value) => value,
38-
None => default
39-
}
40-
}
41-
42-
// ============================================================================
43-
// Result type - represents success or failure
44-
// ============================================================================
45-
4621
type Result<T, E> = Ok(T) | Err(E)
4722

48-
fn is_ok<T, E>(res: Result<T, E>) -> Bool {
49-
match res {
50-
Ok(_) => true,
51-
Err(_) => false
52-
}
53-
}
54-
55-
fn is_err<T, E>(res: Result<T, E>) -> Bool {
56-
match res {
57-
Ok(_) => false,
58-
Err(_) => true
59-
}
60-
}
61-
62-
fn unwrap_result<T, E>(res: Result<T, E>) -> T {
63-
match res {
64-
Ok(value) => value,
65-
Err(_) => {
66-
println("Called unwrap on Err");
67-
// TODO: panic!() once implemented
68-
}
69-
}
70-
}
71-
72-
fn unwrap_or_result<T, E>(res: Result<T, E>, default: T) -> T {
73-
match res {
74-
Ok(value) => value,
75-
Err(_) => default
76-
}
77-
}
78-
7923
// ============================================================================
8024
// List utilities
8125
// ============================================================================

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)