pub struct HostFn { /* private fields */ }Expand description
A host function: a native callback the embedder hands the runtime as a
value, behind the Rc a Value::HostFn carries.
It is what a builtin is, minus the compile-time enum: a name, an arity, and
code. The evaluator gathers arguments against arity and calls f once they
are all there, so a host callback is strict and curried like everything else,
and the host writes an ordinary Rust closure rather than an application
protocol.
The Ctx reaches f so a callback that is handed an Elly callable can call
it back (apply_with) under the same id source; without it a closure the
callback minted would collide with one the program already holds.
Arity is at least 1. A zero-argument callback could never fire — nothing applies a value to no arguments — so a host that wants a constant supplies the value itself.
Implementations§
Source§impl HostFn
impl HostFn
Sourcepub fn new(
name: impl Into<Text>,
arity: usize,
f: impl Fn(&[Value], &Ctx) -> Result<Value, Raised> + 'static,
) -> HostFn
pub fn new( name: impl Into<Text>, arity: usize, f: impl Fn(&[Value], &Ctx) -> Result<Value, Raised> + 'static, ) -> HostFn
A host function called as name, taking arity arguments.
The name is for rendering and diagnostics only — identity is the
allocation, so two HostFns of one name are two functions.
Panics on arity == 0, which is not a function the runtime could ever
call (see the type’s docs).