Stream: beginners

Topic: Top level declaration evaluation


view this post on Zulip Shritesh Bhattarai (Dec 25 2022 at 22:26):

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?

view this post on Zulip Ayaz Hafiz (Dec 25 2022 at 22:28):

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

view this post on Zulip Shritesh Bhattarai (Dec 25 2022 at 22:30):

Thanks. Can/should this change in the future?

view this post on Zulip Brendan Hansknecht (Dec 25 2022 at 22:39):

It is definitely a goal that this will change.

view this post on Zulip Brendan Hansknecht (Dec 25 2022 at 22:42):

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.

view this post on Zulip Richard Feldman (Dec 25 2022 at 22:58):

I want to do comptime in the future

view this post on Zulip Richard Feldman (Dec 25 2022 at 23:00):

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