Initial Commit
This commit is contained in:
789 files changed
+149185
No files matched your search
@@ -0,0 +1,37 @@
|
||||
import std::io;
|
||||
|
||||
# A trait's own functions, declared through it outside it, are named through it,
|
||||
# generic as any function is, and std reads numbers by `real::parse::<T>` and
|
||||
# `integer::parse::<T>`.
|
||||
trait shape {
|
||||
def area(&this): f64;
|
||||
}
|
||||
def shape::total<S>(shapes: span<S>): f64 where S: shape {
|
||||
var sum := 0.0;
|
||||
for &each in shapes { sum += each.area(); }
|
||||
return sum;
|
||||
}
|
||||
def shape::unit(): f64 { return 1.0; }
|
||||
|
||||
trait holder<T> {
|
||||
def held(&this): T;
|
||||
}
|
||||
def holder<T>::pair(x: T): T[2] { return [x, x]; }
|
||||
|
||||
struct square : shape { var side: f64; }
|
||||
def square::area(&this): f64 { return this.side * this.side; }
|
||||
struct cell : holder<i64> { var v: i64; }
|
||||
def cell::held(&this): i64 { return this.v; }
|
||||
|
||||
def main(): void {
|
||||
val squares: square[2] = [square { side = 2.0 }, square { side = 3.0 }];
|
||||
println($"{shape::total(squares[:])} {shape::unit()}");
|
||||
val p := holder::<i64>::pair(21); println($"{p[0] + p[1]}");
|
||||
val c := cell { v = 4 };
|
||||
println($"{c.held()}");
|
||||
val f := shape::unit;
|
||||
println($"{f()}");
|
||||
val r: f32 = real::parse::<f32>("1.5").value;
|
||||
val n: u8 = integer::parse::<u8>("200").value;
|
||||
println($"{r} {n}");
|
||||
}
|
||||
Reference in new issue
Block a user