Under heavy development. This site describes the intended 1.0 language. Current features and performance vary. Development status
Sn50SnThe SnAPI programming language
Development · Evolving reference

MapError

Consumes one wrapper and invokes its safe callback only for the selected case.

Sn is under heavy development. This reference includes the intended 1.0 design; features may be incomplete in the current compiler. Read the status →

Module std.prelude

import std.prelude;

Public declaration contracts and authored examples. A page can document several related symbols. Implementation bodies are excluded.

MapError

pub fn MapError<T, E, F>(mut Value: Result<T, E>, Mapper: fn(E) -> F) -> Result<T, F>
    where T: Movable where E: Movable where F: Movable

Consumes one wrapper and invokes its safe callback only for the selected case.

Unselected owned payloads transfer without copying. The callback is invoked exactly once for the selected case and never for the other case; callback captures/costs keep their own contract. This helper adds no allocation, synchronization or implicit error propagation. Reference payloads remain aliases. T: Exact movable payload representation. E: Exact movable payload representation. F: Exact movable payload representation. Value: Owned wrapper consumed by this call; explicitly move noncopy named inputs. Mapper: Nonnullable safe callback with exact input/output types. Return: Transformed selected payload or preserved opposite case.

MapError

pub fn MapError<T, E, F, C>(mut Value: Result<T, E>, mut Mapper: C) -> Result<T, F>
    where T: Movable where E: Movable where F: Movable where C: UnaryCallable<E, F>, Movable

Consumes one wrapper and invokes its safe callback only for the selected case.

Unselected owned payloads transfer without copying. The callback is invoked exactly once for the selected case and never for the other case; callback captures/costs keep their own contract. This helper adds no allocation, synchronization or implicit error propagation. Reference payloads remain aliases. T: Exact movable payload representation. E: Exact movable payload representation. F: Exact movable payload representation. C: Concrete movable exact safe readonly callback environment. Value: Owned wrapper consumed by this call; explicitly move noncopy named inputs. Mapper: Owned concrete safe callback with exact input/output types. Return: Transformed selected payload or preserved opposite case.

MapError

pub fn MapError<E, F>(mut Value: Result<void, E>, Mapper: fn(E) -> F) -> Result<void, F>
    where E: Movable where F: Movable

Transforms a void-success result error while preserving payloadless success. E: Movable source error representation. F: Movable transformed error representation. Value: Owned void-success wrapper consumed here. Mapper: Nonnullable safe error callback; never invoked on Ok. Return: Preserved Ok or transformed Err without allocation by the helper.

Example

module example.void_result_composition;
from std.prelude import Result, MapResult, MapError, ThenResult, IsSuccess, ResultValue;
fn Answer() -> i32 { return 42; }
fn CheckedAnswer() -> Result<i32, i32> { return .Ok { Value = 42 }; }
fn Describe(Error: i32) -> str { return "failed"; }
fn Main() -> i32 {
    var First: Result<void, i32> = .Ok;
    var Mapped = MapResult<i32, i32>(move(First), Answer);
    if ResultValue(Mapped).Unwrap() != 42 { return 1; }
    var Second: Result<void, i32> = .Ok;
    var Chained = ThenResult<i32, i32>(move(Second), CheckedAnswer);
    if ResultValue(Chained).Unwrap() != 42 { return 2; }
    var Third: Result<void, i32> = .Ok;
    var Preserved = MapError<i32, str>(move(Third), Describe);
    if !IsSuccess(Preserved) { return 3; }
    return 0;
}

MapError

pub fn MapError<E, F, C>(mut Value: Result<void, E>, mut Mapper: C) -> Result<void, F>
    where E: Movable where F: Movable where C: UnaryCallable<E, F>, Movable

Transforms an owned void-result error with a concrete safe environment. E: Movable original error representation. F: Movable transformed error representation. C: Exact movable safe readonly UnaryCallable error environment. Value: Owned void-success wrapper consumed by the call. Mapper: Owned environment invoked exactly once for Err, never for Ok. Return: Transformed error or preserved payloadless success; no helper allocation.

Example

module example.concrete_void_result;
from std.prelude import Result, MapResult, MapError, ThenResult, ResultValue, ResultError;
class Answer {
    priv Calls: mut i32&;
    pub init(Calls: mut i32&) { this.Calls = Calls; }
    pub readonly fn Invoke() -> i32 { this.Calls += 1; return 42; }
}
struct CheckedAnswer {
    pub readonly fn Invoke() -> Result<i32, i32> { return .Ok { Value = 42 }; }
}
struct ErrorMapper {
    pub readonly fn Invoke(Value: i32) -> i32 { return Value + 1; }
}
fn Main() -> i32 {
    var Calls: i32 = 0;
    var First: Result<void, i32> = .Ok;
    var Mapped = MapResult<i32, i32>(move(First), Answer(Calls));
    if Calls != 1 || ResultValue(Mapped).Unwrap() != 42 { return 1; }
    var Second: Result<void, i32> = .Ok;
    var Chained = ThenResult<i32, i32>(move(Second), default(CheckedAnswer));
    if ResultValue(Chained).Unwrap() != 42 { return 2; }
    var Failure: Result<void, i32> = .Err { Error = 7 };
    var Preserved = MapResult<i32, i32>(move(Failure), Answer(Calls));
    if Calls != 1 || ResultError(Preserved).Unwrap() != 7 { return 3; }
    var Error: Result<void, i32> = .Err { Error = 41 };
    var Changed = MapError<i32, i32>(move(Error), default(ErrorMapper));
    if ResultError(Changed).Unwrap() != 42 { return 4; }
    return 0;
}
Documentation snapshot: October 24, 2026 About versions ↗
Searching Development · language & standard library