-
Notifications
You must be signed in to change notification settings - Fork 189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: Is it possible to have an indexer return a callable closure back to Rhai? #951
Comments
Does |
Not in this case since the functions are not registered into rhai. Ideally i wanted not to have to do that, however trying to register them also seems out of possibility, since i dont have the compile time information necessary (arity is dynamic as well) |
Not a normal closure. The only thing you can do is to register an But you need to register your own I can probably add this support into EDIT: If added, the functions are likely required to have a specific signature, e.g. |
Hmm. I'll give you some context so tou can evaluate the need for this better in deciding if you'd like to support this. Having the ability to store a function with a specific signature and having to override the calling mechanism sounds perfect actually. The following is how this looks like in Lua: And the signature for Fn(&Lua, A: FromLuaMulti) -> Result<Function> So it should work, having an FnMut version would be ideal too. The general need arises in the context of function reflection. The moment you forgo compile time knowledge about functions, you gain immense flexibility at the cost of.. well losing the generics et al. I've been working on moving |
It is probably simple to embed a closure within a I can see value in what you're suggesting. Those functions don't need to be exposed to scripts. |
@makspll check out the latest drop with |
That was quick, awesome! |
@makspll I wonder how you find the new feature. Does it do what you want? Please share your usage experience if possible. I'm trying to put some usage scenarios into the Book. |
Hey! So I tried this out, and the actual bit of storing and calling the function works perfectly, thanks again!: Here's me converting my dyn functions to dynamic vals ScriptValue::Function(func) => Dynamic::from(FnPtr::from_fn(
func.name().to_string(),
move |_ctxt: NativeCallContext, args: &mut [&mut Dynamic]| {
let convert_args = args
.iter_mut()
.map(|arg| ScriptValue::from_dynamic(arg.clone()))
.collect::<Result<Vec<_>, _>>()?;
let out = func.call(convert_args, RHAI_CALLER_CONTEXT)?;
out.into_dynamic()
},
)?), Then I can do:
output:
However, the bit that's missing now is allowing to override the dispatch of functions on a type, using an indexer works but is fairly counterintuitive, Ideally I'd be able to either return functions from the indexer, or have a separate
Edit: actually i was returning a function without any curried arguments so maybe that was the problem |
In order to use the style Otherwise you'd need to do Properties default to access via indexers if they are not defined, so the prop shortcut should work just fine. You don't have to always use the indexer notation. |
Hmm I see, would that mean that the object would need to be "pre-populated" with all the functions when it's passed into Rhai? The |
I am trying to attach an indexer function which performs a dynamic lookup on a function registry (basically bunch of
Arc<fn>
's with a specifc signature).I want to be able to:
given:
I know that Dynamic supports
FnPtr
which you can create via aScriptFuncDef
, however that seems to only be appropriate for actual "script defined" functionsThe text was updated successfully, but these errors were encountered: