Closed tprodanov closed 8 months ago
I understand the concept of pure functions and the reasoning behind this problem, but I would not call it "by design". At best, it is a limitation of the optimizer, not a feature.
In any case, would it be possible to add a big warning section to the rhai-rand
documentation and book section? Ideally, would be possible to add optimizer hints that the function is volatile in the future versions?
It probably is possible to mark the function as volatile... But why can't the script be compiled before the library is loaded into the engine?
If that's inconvenient, it still is possible to keep two Engine's, one for compilation and one for execution.
I agree, it is totally possible, and now I did that, but it is really not obvious and a bit error-prone.
True in that respect.
I'll need to think a bit to decide whether it is good to introduce yet another flag to plugin functions, as there are already many.
But would not such flag useful for many use cases? Such as functions that should not be called during compilation, or get current time, as in the docs?
In any case, thank you for answering!
OK, you talked me into it.
I'll add volatile
marker for plugin modules to the next version. Then this will be solved.
As in the title: if I set
engine.set_optimization_level(OptimizationLevel::Full)
and then in the embedded scripts I haverand(1, 10)
, the engine will remove function call and will always produce the same random value in the for loop/function call.For example, in Rhai:
will produce
at default optimization level, and
at full optimization level.