I have a top level declaration that does some heavy computation in the body to generate the result, but it takes no arguments and is fully "constant". Does Roc guarantee that this value is cached/memoized/comptime evaluated/something else? Or is the value re-computed every time it is used somewhere?
Today it is compiled as if it is re-computed every time it is used. Optimization by the LLVM backend may make it truly constant, but that is not guaranteed
Thanks. Can/should this change in the future?
It is definitely a goal that this will change.
Not sure about comp time vs memoized when first called vs memoized during app start up, but it will always be easy to opt out by wrapping a top level value in a lambda that takes no arguments.
I want to do comptime in the future
at least of top-level values, and probably also of anything that can be hoisted to there (e.g. it's declared in the middle of a function, but it doesn't depend on any of that function's arguments)
Last updated: Jul 06 2025 at 12:14 UTC