pub struct GenericFunction { /* private fields */ }Expand description
A named generic function: a set of DispatchMethods sharing one name.
This is the dispatch organ’s central object. Methods are added with
add_method; a call selects the single most
specific applicable primary method (plus around/before/after methods) by
matching arguments through the kernel Shape protocol. The kernel defines
the callable/operation contracts; this type supplies the concrete dispatch.
§Examples
use std::sync::Arc;
use sim_kernel::{Cx, DefaultFactory, ExprKind, NoopEvalPolicy, Shape, Symbol};
use sim_lib_dispatch::{DispatchMethod, GenericFunction, MethodRole};
use sim_shape::{AnyShape, ExprKindShape};
let mut cx = Cx::new(Arc::new(NoopEvalPolicy), Arc::new(DefaultFactory));
let mut generic = GenericFunction::new(Symbol::qualified("demo", "describe"));
// A broad fallback and a more specific string method.
generic.add_method(DispatchMethod::new(
Symbol::qualified("method", "any"),
MethodRole::Primary,
vec![Arc::new(AnyShape) as Arc<dyn Shape>],
Arc::new(|cx: &mut Cx, _| cx.factory().string("any".to_owned())),
)).unwrap();
generic.add_method(DispatchMethod::new(
Symbol::qualified("method", "string"),
MethodRole::Primary,
vec![Arc::new(ExprKindShape::new(ExprKind::String)) as Arc<dyn Shape>],
Arc::new(|cx: &mut Cx, _| cx.factory().string("string".to_owned())),
)).unwrap();
let args = [cx.factory().string("hi".to_owned()).unwrap()];
// The string method is more specific, so it is selected.
let selected = generic.select_primary(&mut cx, &args).unwrap();
assert_eq!(selected.method(), &Symbol::qualified("method", "string"));Implementations§
Source§impl GenericFunction
impl GenericFunction
Sourcepub fn methods(&self) -> &[DispatchMethod]
pub fn methods(&self) -> &[DispatchMethod]
Returns the methods added to this generic function, in insertion order.
Sourcepub fn operation_hints(&self) -> Vec<HintMetadata>
pub fn operation_hints(&self) -> Vec<HintMetadata>
Returns agent-consumable operation hints from the generic and methods.
Sourcepub fn add_method(&mut self, method: DispatchMethod) -> Result<()>
pub fn add_method(&mut self, method: DispatchMethod) -> Result<()>
Adds a method, erroring if one with the same id, role, and arity exists.
Sourcepub fn select_primary(
&self,
cx: &mut Cx,
args: &[Value],
) -> Result<MethodSpecificity>
pub fn select_primary( &self, cx: &mut Cx, args: &[Value], ) -> Result<MethodSpecificity>
Returns the specificity of the most specific applicable primary method.
Errors if no primary method applies, or if the two most specific tie (an ambiguous primary).
Sourcepub fn dispatch_order(&self, cx: &mut Cx, args: &[Value]) -> Result<Vec<Symbol>>
pub fn dispatch_order(&self, cx: &mut Cx, args: &[Value]) -> Result<Vec<Symbol>>
Returns the method ids in the order call runs them.
The plan is around (most specific first), before (most specific first), the selected primary, then after (least specific first).
Sourcepub fn inspect_specificity(
&self,
cx: &mut Cx,
args: &[Value],
) -> Result<Vec<MethodSpecificity>>
pub fn inspect_specificity( &self, cx: &mut Cx, args: &[Value], ) -> Result<Vec<MethodSpecificity>>
Returns every applicable method’s specificity, ordered for display.
Sorts by combination role, then most specific first, then method id;
backs the dispatch inspect operation.
Sourcepub fn call(&self, cx: &mut Cx, args: &[Value]) -> Result<Value>
pub fn call(&self, cx: &mut Cx, args: &[Value]) -> Result<Value>
Dispatches the call: runs the combination and returns the primary result.
Executes around, before, the selected primary, and after methods in order; errors if no applicable primary method exists.
Sourcepub fn call_for_profile(
&self,
cx: &mut Cx,
_profile: &Symbol,
args: &[Value],
) -> Result<Value>
pub fn call_for_profile( &self, cx: &mut Cx, _profile: &Symbol, args: &[Value], ) -> Result<Value>
Dispatches the call for a named language profile.
One generic function serves every profile: dispatch is profile-neutral,
so this delegates to call. The profile
argument lets profile-specific surfaces share a single generic.