37 lines
1.3 KiB
Plaintext
37 lines
1.3 KiB
Plaintext
import std::io;
|
|
struct counter { var n: int; }
|
|
def counter::bump(this: &mut counter, by: int = 1, times: int = by): void {
|
|
this.n = this.n + by * times;
|
|
}
|
|
def counter::scaled(this: &counter, by: int = this.n): int { return this.n * by; }
|
|
struct box<T> { var it: T; }
|
|
def box<T>::make(it: T, tag: span<u8> = "none"): string { return $"{it} {tag}"; }
|
|
var calls: int = 0;
|
|
def next(): int { calls = calls + 1; return 5; }
|
|
def twice(a: int, b: int = a): int { return a + b; }
|
|
def told(cond: bool, text: span<u8> = source_of(cond), at: site = call_site()): string {
|
|
return $"{at.line}:{at.column} {text} {cond}";
|
|
}
|
|
def greet(name: span<u8> = "world", mark: span<u8> = "!"): string { return $"hi {name}{mark}"; }
|
|
def sized(n: int = 3): int { return n * n; }
|
|
def within<T>(v: T, limit: int): string where T: equatable {
|
|
return $"{told(limit > 3)} {told(v == v)}";
|
|
}
|
|
def main(): void {
|
|
var c := counter { n = 0 };
|
|
c.bump();
|
|
c.bump(3);
|
|
c.bump(2, 4);
|
|
println($"{c.n} {c.scaled()}");
|
|
println(box::<int>::make(9));
|
|
println(box::<int>::make(9, "tag"));
|
|
println($"{twice(next())} {calls}");
|
|
println(greet());
|
|
println(greet("sakura", "?"));
|
|
println(told(1 == 2));
|
|
println(within::<int>(1, 2));
|
|
println(within::<span<u8>>("a", 9));
|
|
const k := sized();
|
|
println($"{k}");
|
|
}
|