If a closure captures a parameter and that closure is used in-place (not stored somewhere for later use), does it have to create copies of the captures?
doSomething = \model ->
doSomethingElse model.item (\item ->
{model & item})
does this have to copy model
?
It will copy any non-refcounted captures.
For refcounted captures, it will increment the refcount and hold a reference
How can you know if it's a refcounted capture?
simple fixed sized types like i32
, f32
or a record or tuple containing only these are not refcounted, while things like Str
or List a
or records or tuples containing these are
Just depends on the data type. Anything on the heap is refcount: Box, Str, List, recursive tag...I think that's it in roc. Everything else is not
A record or tuple would be copied, but all of its refcounted fields would just be referenced.
So a gigantic record could be problematic.
So if it's a record it does make a copy, but if the record has a List field, that list points to the same memory as the thing it was copied from. Makes sense thanks!
Yep
Last updated: Jul 06 2025 at 12:14 UTC