Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-19 04:54:17 -06:00
commit f086abcea6
789 files changed
+149185

No files matched your search

@@ -0,0 +1,68 @@
import std::io;
import std::containers;
# Where a value goes says what happens to it: a reference type lends it, and
# any other type gives it, copied where it closes nothing. Only `&` or `&mut`
# written, or a reference parameter at a call, makes a reference, and
# references never stack.
struct point { var x: i64; var y: i64; }
def larger<T>(a: T, b: T): T where T: ordered { return a < b ? b : a; }
def bump(n: &mut i64): void { n += 1; }
def main(): void {
# A binding copies an element, and `&` refers to it.
var xs := vector::<i64>::new();
xs.append(1);
xs.append(2);
val copied := xs[0];
val lent := &xs[0];
xs[0] = 7;
println($"{copied} {lent}");
# So does `for`, and `for &mut` writes each in place.
var total: i64 = 0;
for x in xs { total += x; }
for &mut x in xs { x *= 10; }
println($"{total} {xs[0]} {xs[1]}");
# Inference sees values, so `larger` gives a copy, not an element.
val big := larger(xs[0], xs[1]);
xs[1] = 0;
println($"{big} {std::math::sqrt(16.0)}");
# A pattern copies what a reference reaches, and `&` refers to it.
var marks := dict::<u64, point>::new();
marks.set(1, point { x = 1, y = 2 });
if marks.get(&1) is some(&mut p) { p.x = 5; }
if marks.get(&1) is some(seen) { println($"{seen.x} {seen.y}"); }
# `&` of a reference is that reference, wherever it is held.
var m: maybe<i64> = 3 as i64;
if m is some(&mut held) {
bump(&mut held);
bump(held);
val again := &mut held;
again += 1;
}
println($"{m.some}");
# Assigning writes through a reference, and only a reference written as one
# points it elsewhere.
var a: i64 = 1;
var b: i64 = 2;
var r := &mut a;
val s := &mut b;
r = s;
r = &mut b;
r = 9;
println($"{a} {b}");
# A reference written as one is a pointer where one is expected.
var c := point { x = 3, y = 4 };
val p: *mut point = &mut c;
p[0].y = 8;
val q := address(c);
println($"{c.y} {q[0].x} {through(q).y}");
}