What is a :thunk expression head?

I’ve been playing around with rewriting julia expressions such that they evaluate lazily, while supporting arbitrary syntax. This is really useful to me for statically constructing complex computational graphs where only a subset of computations will ultimately be needed / performed.

I’ve noticed that there is already a notion of a thunk in Julia AST, although it’s not documented much. This link shows an example :thunk head:

https://docs.julialang.org/en/v1/devdocs/reflection/#Expansion-and-lowering

While this link references the idea of a “toplevel thunk”:

https://docs.julialang.org/en/v1/devdocs/ast/#MethodInstance

I can see that :thunk is used extensively in Julia itself, although I don’t fully grok if it’s “okay” for me as an end user to rewrite expressions with a :thunk head or if this is a Bad Idea™.

Would anyone be able to point me to anything I could read that might be illuminating, or have a moment to explain / offer context?

Many thanks!

2 Likes

:thunk is an internal marker that lowering uses to indicate that something is no longer modifiable surface level syntax, and thus is ready for immediate execution. It should not appear in parser or macro contexts.

3 Likes