Skip to main content

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.

fn-call.stof
#[main]
fn main() {
  const func: fn = (name: str): str => "Hi, " + name;
  pln(func.call("Bob"), Fn.call(func, "Bob"), func("Bob"));
}
Output

Fn.call_expanded(func: fn, ..) -> unknown

Same as call, but a collection argument gets expanded into positional arguments instead of passed as one value.

fn-call-expanded.stof
#[main]
fn main() {
  const func: fn = (name: str): str => "Hi, " + name;
  pln(func.call_expanded(["Bob"]));
}
Output

Binding

Fn.bind(func: fn, to: obj) -> bool

Moves a function onto a different object — self inside it now refers to to.

fn-bind.stof
#[main]
fn main() {
  const func = (): str => self.msg ?? 'dne';
  const to = new { msg: 'hi' };
  func.bind(to);
  pln(func());
}
Output

Introspection

Fn.name(func: fn) -> str

fn-name.stof
fn hi() -> str { 'hi' }

#[main]
fn main() {
  const func: fn = self.hi;
  pln(func.name());
}
Output

Fn.params(func: fn) -> list

A list of (name, type) tuples.

fn-params.stof
fn hi(a: int) {}

#[main]
fn main() {
  const func: fn = self.hi;
  pln(func.params());
}
Output

Fn.return_type(func: fn) -> str

fn-return-type.stof
fn hi() -> int { 42 }

#[main]
fn main() {
  const func: fn = self.hi;
  pln(func.return_type());
}
Output

Fn.is_async(func: fn) -> bool

Shorthand for checking whether the async attribute is present.

fn-is-async.stof
async fn hi() -> str { 'hi' }

#[main]
fn main() {
  const func: fn = self.hi;
  pln(func.is_async());
}
Output

Fn.attributes(func: fn) -> map

fn-attributes.stof
#[greeting]
fn hi() -> str { 'hi' }

#[main]
fn main() {
  const func: fn = self.hi;
  pln(str(func.attributes()));
}
Output

Fn.has_attribute(func: fn, name: str) -> bool

fn-has-attribute.stof
#[greeting]
fn hi() -> str { 'hi' }

#[main]
fn main() {
  const func: fn = self.hi;
  pln(func.has_attribute("greeting"));
}
Output

Fn.obj(func: fn) -> obj

The first object this function is attached to.

fn-obj.stof
fn hi() -> str { 'hi' }

#[main]
fn main() {
  const func: fn = self.hi;
  pln(func.obj() == self);
}
Output

Fn.objs(func: fn) -> list

Every object this function is attached to — usually just one, unless it's been shared via Data.attach.

fn-objs.stof
fn hi() -> str { 'hi' }

#[main]
fn main() {
  const func: fn = self.hi;
  pln(func.objs().len());
}
Output

Fn.data(func: fn) -> data

The underlying Data handle — see the Data library.

fn-data.stof
fn hi() -> str { 'hi' }

#[main]
fn main() {
  const func: fn = self.hi;
  pln(func.data().exists());
}
Output

Fn.id(func: fn) -> str

Shorthand for func.data().id().

fn-id.stof
fn hi() -> str { 'hi' }

#[main]
fn main() {
  const func: fn = self.hi;
  pln(func.id() == func.data().id());
}
Output