Closed soerenmeier closed 1 year ago
Hello, the reason it is giving you that error is the print function[^1], sort of confusingly, does not actually do any printing, instead it returns a string representing the given value. What you want to do instead is register the appropriate functions outlined here. For example if you wanted to override just print
level calls for arrays you could do something like so:
engine.register_fn("print", |arr: &mut Array| -> String {
arr.iter()
.map(|i| i.to_string())
.fold(String::new(), |a, b| a + b.as_str() + "\n")
.trim_end()
.to_string()
});
Usage:
print([\"abc\", \"def\"])
abc
def
[^1]: well it does, but not the overridable one, see on_print for overriding actual printing
The print
or to_string
functions take data and return a text representation as a string.
That's why you get an error... Rhai expects you give it a string. Actually am empty string would do just fine.
Rhai takes this string and displays it, usually. This behaviour is controlled by the on_print
callback on the Engine, which you can also override.
Thanks for quick response, that makes sense. I will update my function accordingly.
I wanted to add a print function which takes an array and prints each element on a new line. When executing print with an array i now get the error
Error: ErrorMismatchOutputType("string", "()", 1:1)
. See this commit where i added the following code to reproduce the issue: