29 lines
1.1 KiB
Plaintext
29 lines
1.1 KiB
Plaintext
import std::io;
|
|
import system;
|
|
|
|
# `@inline` functions, methods, and properties are copied where they are
|
|
# called, and give what a call would: called through a trait reference or a
|
|
# function value, or calling themselves, they are called. A fault within a copy
|
|
# names the function, then where it was called.
|
|
trait shape { def area(&this): i64; }
|
|
struct square : shape { var side: i64; }
|
|
@inline def square::area(&this): i64 { return this.side * this.side; }
|
|
@inline val square::perimeter: i64 { get => 4 * this.side; }
|
|
|
|
@inline def twice<T>(x: T): T where T: numeric { return x + x; }
|
|
@inline def halve(x: i64): i64 { val half := x / 2; return half; }
|
|
@inline def depth(n: u64): u64 { return n == 0 ? 0 : depth(n - 1) + 1; }
|
|
@inline def within(at: u64, length: u64): u64 {
|
|
if at >= length { abort(); }
|
|
return at;
|
|
}
|
|
|
|
def main(): void {
|
|
val s := square { side = 3 };
|
|
val r: &shape = &s;
|
|
val f: def(i64): i64 = halve;
|
|
println($"{s.area()} {s.perimeter} {r.area()} {twice(21)} {twice(1.25)} {f(9)} {depth(5)}");
|
|
print($"{within(2, 3)} ");
|
|
print($"{within(7, 3)} ");
|
|
}
|