Skip to main content

sim_lib_lang_julia/
generic.rs

1use std::sync::Arc;
2
3use sim_kernel::{Cx, Result, Shape, Symbol, Value};
4use sim_lib_dispatch::{DispatchMethod, GenericFunction, MethodBody, MethodRole};
5
6/// Julia multiple-dispatch function backed by the shared dispatch organ.
7///
8/// Thin profile wrapper over [`GenericFunction`]: it presents the Julia
9/// surface, while argument specificity and method selection remain
10/// dispatch-organ behavior rather than kernel contract.
11pub struct JuliaFunction {
12    generic: GenericFunction,
13}
14
15impl JuliaFunction {
16    /// Creates an empty Julia function with the given name.
17    pub fn new(name: Symbol) -> Self {
18        Self {
19            generic: GenericFunction::new(name),
20        }
21    }
22
23    /// Adds a primary method keyed by its name and argument shapes.
24    pub fn add_method(
25        &mut self,
26        method: Symbol,
27        argument_shapes: Vec<Arc<dyn Shape>>,
28        body: MethodBody,
29    ) -> Result<()> {
30        self.generic.add_method(DispatchMethod::new(
31            method,
32            MethodRole::Primary,
33            argument_shapes,
34            body,
35        ))
36    }
37
38    /// Returns the applicable methods ordered from most to least specific.
39    pub fn dispatch_order(&self, cx: &mut Cx, args: &[Value]) -> Result<Vec<Symbol>> {
40        self.generic.dispatch_order(cx, args)
41    }
42
43    /// Dispatches under the Julia profile and returns the selected method's result.
44    pub fn call(&self, cx: &mut Cx, args: &[Value]) -> Result<Value> {
45        self.generic
46            .call_for_profile(cx, &crate::julia_profile_symbol(), args)
47    }
48}