Function Library (Fn)
Linked to the fn type — the mechanics behind everything on the Functions and Attributes Learn pages.
Calling
Fn::call(func: fn, ..) -> unknown
func.call(...), Fn::call(func, ...), and func(...) are all equivalent.
#[main]
fn main() {
const func: fn = (name: str): str => "Hi, " + name;
pln(func.call("Bob"), ' ', Fn::call(func, "Bob"), ' ', func("Bob"));
}Fn::call_expanded(func: fn, ..) -> unknown
Same as call, but a collection argument gets expanded into positional arguments instead of passed as one value.
#[main]
fn main() {
const func: fn = (name: str): str => "Hi, " + name;
pln(func.call_expanded(["Bob"]));
}Binding
Fn::bind(func: fn, to: obj) -> bool
Moves a function onto a different object — self inside it now refers to to.
#[main]
fn main() {
const func = (): str => self.msg ?? 'dne';
const to = new { msg: 'hi' };
func.bind(to);
pln(func());
}Introspection
Fn::name(func: fn) -> str
fn hi() -> str { 'hi' }
#[main]
fn main() {
const func: fn = self.hi;
pln(func.name());
}Fn::params(func: fn) -> list
A list of (name, type) tuples.
fn hi(a: int) {}
#[main]
fn main() {
const func: fn = self.hi;
pln(func.params());
}Fn::return_type(func: fn) -> str
fn hi() -> int { 42 }
#[main]
fn main() {
const func: fn = self.hi;
pln(func.return_type());
}Fn::is_async(func: fn) -> bool
Shorthand for checking whether the async attribute is present.
async fn hi() -> str { 'hi' }
#[main]
fn main() {
const func: fn = self.hi;
pln(func.is_async());
}Fn::attributes(func: fn) -> map
#[greeting]
fn hi() -> str { 'hi' }
#[main]
fn main() {
const func: fn = self.hi;
pln(str(func.attributes()));
}Fn::has_attribute(func: fn, name: str) -> bool
#[greeting]
fn hi() -> str { 'hi' }
#[main]
fn main() {
const func: fn = self.hi;
pln(func.has_attribute("greeting"));
}Fn::set_attribute(func: fn, name: str, value: unknown = null) -> bool
Adds an attribute to this function (or replaces its value). A null value is an attribute with no value, like #[main]. A sealed function's attributes can't change.
fn hi() -> str { 'hi' }
#[main]
fn main() {
const func: fn = self.hi;
func.set_attribute("greeting");
pln(func.has_attribute("greeting"));
}Fn::remove_attribute(func: fn, name: str) -> bool
Removes an attribute from this function. Returns true if it had the attribute.
#[greeting]
fn hi() -> str { 'hi' }
#[main]
fn main() {
const func: fn = self.hi;
pln(func.remove_attribute("greeting"), ' ', func.has_attribute("greeting"));
}Fn::obj(func: fn) -> obj
The first object this function is attached to.
fn hi() -> str { 'hi' }
#[main]
fn main() {
const func: fn = self.hi;
pln(func.obj() == self);
}Fn::objs(func: fn) -> list
Every object this function is attached to — usually just one, unless it's been shared via Data.attach.
fn hi() -> str { 'hi' }
#[main]
fn main() {
const func: fn = self.hi;
pln(func.objs().len());
}Fn::data(func: fn) -> data
The underlying Data handle — see the Data library.
fn hi() -> str { 'hi' }
#[main]
fn main() {
const func: fn = self.hi;
pln(func.data().exists());
}Fn::id(func: fn) -> str
Shorthand for func.data().id().
fn hi() -> str { 'hi' }
#[main]
fn main() {
const func: fn = self.hi;
pln(func.id() == func.data().id());
}