Initial Commit
This commit is contained in:
706 files changed
+135430
No files matched your search
@@ -0,0 +1,99 @@
|
||||
import std::io;
|
||||
|
||||
# A `function` holds one callable of a signature, whatever its type, and owns it:
|
||||
# a named function, a lambda, a method taken through its type, a functor in its
|
||||
# buffer, or a larger one in a box. A list of handlers mixes them, and a functor
|
||||
# holding a `string` is kept past the function making it, as no lambda can be.
|
||||
|
||||
struct event { var name: strview; var weight: i64; }
|
||||
|
||||
struct tally : invokable<def(&event): void> { var seen: i64; var weighed: i64; }
|
||||
|
||||
def tally::invoke(&mut this, e: &event): void {
|
||||
this.seen++;
|
||||
this.weighed += e.weight;
|
||||
}
|
||||
|
||||
# Larger than a function's buffer, so it is held in a box.
|
||||
struct journal : invokable<def(&event): void>, owning {
|
||||
var lines: vector<string>;
|
||||
var label: string;
|
||||
var spare: u64[2];
|
||||
}
|
||||
|
||||
def journal::invoke(&mut this, e: &event): void {
|
||||
this.lines.append($"{this.label}: {e.name}");
|
||||
}
|
||||
|
||||
# A journal moved from holds nothing, and says nothing as it closes.
|
||||
def journal::close(&mut this): void {
|
||||
if this.label.length != 0 {
|
||||
println($"journal closed with {this.lines.length} lines");
|
||||
}
|
||||
this.lines.close();
|
||||
this.label.close();
|
||||
}
|
||||
|
||||
def announce(e: &event): void {
|
||||
println($"announced {e.name}");
|
||||
}
|
||||
|
||||
struct greeter : invokable<def(strview): string> { var greeting: string; }
|
||||
|
||||
def greeter::invoke(&mut this, name: strview): string {
|
||||
return $"{this.greeting}, {name}";
|
||||
}
|
||||
|
||||
# The string lives in the functor the function owns, so it outlives `greeting_of`.
|
||||
def greeting_of(word: strview): function<def(strview): string> {
|
||||
return function::new(greeter { greeting = string::from(word, nothing {}) });
|
||||
}
|
||||
|
||||
def event::loudly(&this): string {
|
||||
return $"{this.name}!";
|
||||
}
|
||||
|
||||
struct button {
|
||||
var label: strview;
|
||||
var on_click: function<def(): void>;
|
||||
}
|
||||
|
||||
def main(): void {
|
||||
var heard := 0;
|
||||
var handlers := vector::<function<def(&event): void>>::new();
|
||||
handlers.append(function::new(tally {}));
|
||||
handlers.append(function::new(announce));
|
||||
handlers.append(function::new(def(e: &event) { heard++; }));
|
||||
handlers.append(function::new(journal { label = string::from("log", nothing {}) }));
|
||||
|
||||
val events: event[3] = [event { name = "open", weight = 2 }, event { name = "save", weight = 5 }, event { name = "close", weight = 1 }];
|
||||
for &e in events {
|
||||
for &h in handlers { h(&e); }
|
||||
}
|
||||
if handlers[0].get::<tally>() is some(t) {
|
||||
println($"tally saw {t.seen} weighing {t.weighed}");
|
||||
}
|
||||
if handlers[3].get::<journal>() is some(&j) {
|
||||
println($"journal holds {j.lines.length}: {j.lines[2]}");
|
||||
}
|
||||
println($"the lambda heard {heard}, and the second holds a tally: {handlers[1].get::<tally>() is some(_)}");
|
||||
|
||||
val hello := greeting_of("hello");
|
||||
println(hello("sakura"));
|
||||
|
||||
# A method taken through its type takes its receiver first.
|
||||
val loud: function<def(&event): string> = function::new(event::loudly);
|
||||
println(loud(&events[1]));
|
||||
|
||||
# Unset until given, as a field needs no value where its struct is made.
|
||||
var b := button { label = "OK" };
|
||||
println($"{b.label} handled: {b.on_click.holds()}");
|
||||
b.on_click = function::new(def() { println("clicked"); });
|
||||
if b.on_click.holds() { b.on_click(); }
|
||||
|
||||
# Moved, it leaves the one moved from empty.
|
||||
var moved := move(&mut handlers[3]);
|
||||
println($"moved: {handlers[3].holds()} {moved.holds()}");
|
||||
moved.close();
|
||||
println("done");
|
||||
}
|
||||
Reference in new issue
Block a user