nx_stdlib.nx source
↩ module page · 34 lines · 1137 B
1// stdlib.nx -- NishiLang's built-in prelude.
2//
3// Any file that `import "nx_stdlib.nx"` gets Option<T> + Result<T, E>
4// as real generic tagged enums, backed by the shadow-struct lowering
5// in parse.c/parse.nx. Stage 2 constructor / use:
6//
7// let some_v: *Option<i64> = Option::Some(42)
8// let none_v: *Option<i64> = Option::None
9//
10// match some_v {
11// Option::Some(x) => { ... x ... },
12// Option::None => { ... },
13// }
14//
15// The shadow struct is `{ tag: i64, payload: i64 }` for every
16// instantiation -- T is erased into an 8-byte payload slot at this
17// stage, so any T that fits in 8 bytes (primitives, pointers, small
18// enums) rides the shared layout. Types stay distinct at the IR
19// surface (Option$i64 vs Result$i64$i64 etc.) so imports do not
20// collide. Stage 3 introduces variant-sized unions for payloads
21// wider than 8 bytes.
22//
23// Keep this file declarations-only -- no functions, no main -- so
24// callers can import it alongside other libraries cleanly.
25
26enum Option<T> {
27 None,
28 Some(T),
29}
30
31enum Result<T, E> {
32 Ok(T),
33 Err(E),
34}