#include "system-macros.x"/* Dynamic values cross typed boundaries; native types still matter. is tests an exact tag. Conversion can succeed without that tag matching. */int main(void) { Var value = 42; double converted = value; printf("Int tag: %d\nDouble tag: %d\nConverted to double: %.1f\n", value is int, value is double, converted); Var wide = 42L; puts($dedent(%" Numeric equality: $(value == wide) Identity: $(value === wide)")); return 0;}
Actual output
Expected output
Int tag: 1
Double tag: 0
Converted to double: 42.0
Numeric equality: 1
Identity: 0
Run locally: x2c run examples/tours/essentials/values.x
Immutable values and shared objects
Change an array through a second handle and watch the change appear through
the first.
/* Classes choose representations, not an object hierarchy. A record copies its fields. A pointer class copies its handle. Reference methods borrow the original record; no & is needed at the call. */class Point { int x; int y; };class Node { int value; } *;static void Point.move(Point &p, int dx, int dy) { p.x += dx; p.y += dy; }int main(void) { Point point = Point.new(2, 3), copy = point; point.move(1, 1); Node node = auto Node.new(7); Node same = node; same.value++; printf("Moved point: (%d, %d)\nCopied point: (%d, %d)\nShared node: %d\n", point.x, point.y, copy.x, copy.y, node.value); // A record containing an Array would copy that handle, not its contents. return 0;}
#include "system-macros.x"/* Protocol adoption gives ordinary types shared operations. Set reuses Map storage. These methods make + a union, not arithmetic. */typedef Map Set;static Set Set.add(Set a, Set b) => a.copy().merge(b);protocol Map(T) { T T.add(T, T); }protocol Map(Set);int main(void) { Set a = {apple: 1, pear: 1}, b = {pear: 1, bread: 1}; Set combined = a + b; puts($dedent(%" Union: $(combined.keys().list().sort().repr()) Original set size: $(a.len())")); return 0;}
Actual output
Expected output
Union: (apple bread pear)
Original set size: 2
Run locally: x2c run examples/tours/essentials/operators.x
Patterns and templates
Extract two coordinates and the remaining flags, then try the same
coordinate pattern with doubles.
/* A pattern states the structure you need and names its parts. Typed captures test tags rather than converting; a double does not match ?(int n). Templates then build a result without manual List plumbing. */int main(void) { List command = %(move (3 4) fast quiet); match (command) { case %(move (?(int x) ?(int y)) *flags): puts(%"Matched command: $(%(at $x $y @flags).repr())"); } List real = %(move (3.0 4.0)); printf("Doubles match the int pattern: %d\n", real ~= %(move (?(int x) ?(int y)))); return 0;}
Actual output
Expected output
Matched command: (at 3 4 fast quiet)
Doubles match the int pattern: 0
Run locally: x2c run examples/tours/essentials/patterns.x
Lifetime and cleanup
Keep an array past the end of its allocation region, then watch cleanup run
before the error handler.
#include "system-macros.x"/* The same language computes values and constructs source at compile time. meta enables compilation-time calls; $ requests one. Macros capture syntax. Declared macro locals are private; free names normally come from the caller. */meta static int cube(int n) => n * n * n;macro Decorator $twice(Stmt $body) { { for (int repeat = 0; repeat < 2; repeat++) $body }}keyword twice $twice;int main(void) { int n = 3, total = 0; twice { total += cube(n); } printf("Compile-time cube: %d\nRuntime total: %d\n", $cube(3), total); // Lists can also be executable data. Lisp evaluates them at runtime. List expression = %(+ 2 3); Lisp lisp = auto Lisp.new(); puts($dedent(%" Lisp result: $(lisp.eval(expression)) Native result: $(2 + 3)")); return 0;}
#include "system-macros.x"/* Threads share references; joining does not transfer their ownership. The parent owns counts. Only the consumer writes it, and main reads after both workers finish. Immutable text crosses the channel without copying. */int main(void) { Channel lines = auto Channel.new(<string>, 2); Map counts = {}; Thread.run([ %!() => { defer lines.close(); foreach (String line in %("tea and cake" "more cake")) lines.send(line); }, %!() => { foreach (String line in lines) foreach (String word in line.words()) counts[word] += 1; } ]); puts($dedent(%" Cake: $(counts["cake"]) Tea: $(counts["tea"])")); return 0;}
Actual output
Expected output
Cake: 2
Tea: 1
Run locally: x2c run examples/tours/essentials/threads.x