-
Hey, how to make all array backed by engine.register_custom_syntax(
["var", "$ident$", "=", "$expr$"],
true,
|context, inputs| {
let var_name = inputs[0].get_string_value().unwrap();
let expr = &inputs[1];
// Evaluate the expression
let value = context.eval_expression_tree(expr)?;
// Push a new variable into the scope if it doesn't already exist.
// Otherwise just set its value.
if !context.scope().is_constant(var_name).unwrap_or(false) {
if value.is_array() {
let series = NdArrayWrapper::from(value);
context.scope_mut().set_value(var_name, series);
} else {
context.scope_mut().set_value(var_name, value);
}
Ok(Dynamic::UNIT)
} else {
Err(format!("Variable `{var_name}` is declared as constant",)
.into())
}
},
)?; It works great but only when used as var a = [1, 2, 3] not as standalone array [1, 2, 3] How can I achieve this? |
Beta Was this translation helpful? Give feedback.
Answered by
schungx
Nov 3, 2024
Replies: 1 comment 3 replies
-
It would be quite difficult to override the standard arrays in Rhai, as arrays have lots of built-in handling. You can consider using a prefix, such as: let x = ND#[1, 2, 3]; In that case, the custom syntax starts with |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
rawhuul
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It would be quite difficult to override the standard arrays in Rhai, as arrays have lots of built-in handling.
You can consider using a prefix, such as:
In that case, the custom syntax starts with
ND#
and the value returned is anNdArray
.