Skip to content

Commit 58c8eb8

Browse files
fix(stdlib): option.affine — last STAGE-A file; 19/19 (#128) (#192)
- take/get_or_insert: Rust-style `&mut Option<T>` -> AffineScript `mut opt: Option<T>` ownership-prefix param (grammar has no `&mut`) - drop prelude `map` from the import (option defines its own Option `map`; prelude's is the list map — name conflict, same class as collections#191) - transpose/map_filter: rewrite the list-map with the file's own for-loop ++ idiom instead of the (shadowed) map - cat_options: Unit statement-block match arm so it matches the `None => {}` arm (a bare assignment expr is typed as its RHS -> Array vs Unit across arms; collect already works via its Never arm) - collect/cat_options: reassigned `values` -> `let mut` stdlib 18->19/19 — STAGE A coherence complete. 233/233 dune test, zero regression. Refs #128 Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 9513d30 commit 58c8eb8

1 file changed

Lines changed: 23 additions & 8 deletions

File tree

stdlib/option.affine

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
module option;
77

88
// `Option`/`Result` types + constructors are owned by `prelude` (ADR-011).
9-
use prelude::{Option, Some, None, Result, Ok, Err, map};
9+
// `map` is defined locally below as the Option map (f, Option<T>);
10+
// it is NOT imported from prelude (prelude's `map` is the list map
11+
// with a different signature — importing both would conflict).
12+
use prelude::{Option, Some, None, Result, Ok, Err};
1013

1114
// This module is the single canonical home for the Option *operations*
1215
// (is_some/is_none/unwrap/unwrap_or/map/filter/contains/…). #133 removed
@@ -184,14 +187,20 @@ fn unzip<A, B>(opt: Option<(A, B)>) -> (Option<A>, Option<B>) {
184187
/// Transpose Option of list to list of Option
185188
fn transpose<T>(opt: Option<[T]>) -> [Option<T>] {
186189
match opt {
187-
Some(list) => map(fn(x) => Some(x), list),
190+
Some(list) => {
191+
let mut result = [];
192+
for x in list {
193+
result = result ++ [Some(x)];
194+
}
195+
result
196+
},
188197
None => []
189198
}
190199
}
191200

192201
/// Collect list of Options into Option of list (None on any None)
193202
fn collect<T>(opts: [Option<T>]) -> Option<[T]> {
194-
let values = [];
203+
let mut values = [];
195204
for opt in opts {
196205
match opt {
197206
Some(value) => values = values ++ [value],
@@ -203,10 +212,13 @@ fn collect<T>(opts: [Option<T>]) -> Option<[T]> {
203212

204213
/// Filter out Nones from list
205214
fn cat_options<T>(opts: [Option<T>]) -> [T] {
206-
let values = [];
215+
let mut values = [];
207216
for opt in opts {
208217
match opt {
209-
Some(value) => values = values ++ [value],
218+
// Statement-block arm so the arm is Unit-typed and matches the
219+
// `None => {}` arm (a bare assignment expression is typed as its
220+
// RHS, which would clash Array vs Unit across the arms).
221+
Some(value) => { values = values ++ [value]; },
210222
None => {}
211223
}
212224
}
@@ -215,7 +227,10 @@ fn cat_options<T>(opts: [Option<T>]) -> [T] {
215227

216228
/// Map list with function returning Option, filtering Nones
217229
fn map_filter<T, U>(f: T -> Option<U>, list: [T]) -> [U] {
218-
let results = map(f, list);
230+
let mut results = [];
231+
for x in list {
232+
results = results ++ [f(x)];
233+
}
219234
cat_options(results)
220235
}
221236

@@ -317,14 +332,14 @@ fn replace_none<T>(opt: Option<T>, replacement: Option<T>) -> Option<T> {
317332
}
318333

319334
/// Take value from Option, leaving None
320-
fn take<T>(opt: &mut Option<T>) -> Option<T> {
335+
fn take<T>(mut opt: Option<T>) -> Option<T> {
321336
let result = opt;
322337
opt = None;
323338
result
324339
}
325340

326341
/// Insert value into Option if None
327-
fn get_or_insert<T>(opt: &mut Option<T>, value: T) -> T {
342+
fn get_or_insert<T>(mut opt: Option<T>, value: T) -> T {
328343
match opt {
329344
Some(x) => x,
330345
None => {

0 commit comments

Comments
 (0)