Initial Commit
This commit is contained in:
789 files changed
+149185
No files matched your search
@@ -0,0 +1,27 @@
|
||||
import std::io;
|
||||
struct shape {}
|
||||
trait shape::measured { def area(&this): i64; def twice(&this): i64 { return this.area() * 2; } }
|
||||
struct square : shape::measured { var side: i64; }
|
||||
def square::area(&this): i64 { return this.side * this.side; }
|
||||
struct bag<T> { var items: T[2]; }
|
||||
trait bag<T>::visitor { def visit(&mut this, item: &T): void; }
|
||||
trait bag<T>::mapper<U> { def map(&this, item: &T): U; }
|
||||
def bag<T>::walk(&this, v: &mut bag<T>::visitor): void {
|
||||
var i: u64 = 0;
|
||||
while i < 2 { v.visit(&this.items[i]); i++; }
|
||||
}
|
||||
def bag<T>::first<U, M>(&this, m: &M): U where M: bag<T>::mapper<U> { return m.map(&this.items[0]); }
|
||||
struct summer : bag<i64>::visitor { var total: i64; }
|
||||
def summer::visit(&mut this, item: &i64): void { this.total += item; }
|
||||
struct halver {}
|
||||
def halver::map(&this, item: &i64): f64 { return item as f64 / 2.0; }
|
||||
def measure<T>(x: &T): i64 where T: shape::measured { return x.area(); }
|
||||
def twice_or<T>(x: &T): i64 { if const T is shape::measured { return x.twice(); } return -1; }
|
||||
def main(): void {
|
||||
val s := square{ 3 };
|
||||
val m: &shape::measured = &s;
|
||||
val b := bag::<i64>{ [5, 6] };
|
||||
var total := summer{ 0 };
|
||||
b.walk(&mut total);
|
||||
println($"{measure(&s)} {m.twice()} {twice_or(&s)} {twice_or(&b)} {total.total} {b.first::<f64, halver>(&halver{})}");
|
||||
}
|
||||
Reference in new issue
Block a user