Stream: beginners

Topic: Are closure captures ref-counted?


view this post on Zulip Jared Cone (Nov 08 2024 at 17:27):

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?

view this post on Zulip Brendan Hansknecht (Nov 08 2024 at 20:24):

It will copy any non-refcounted captures.

view this post on Zulip Brendan Hansknecht (Nov 08 2024 at 20:24):

For refcounted captures, it will increment the refcount and hold a reference

view this post on Zulip Jared Cone (Nov 08 2024 at 20:29):

How can you know if it's a refcounted capture?

view this post on Zulip Luke Boswell (Nov 08 2024 at 20:36):

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

view this post on Zulip Brendan Hansknecht (Nov 08 2024 at 20:36):

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

view this post on Zulip Brendan Hansknecht (Nov 08 2024 at 20:37):

A record or tuple would be copied, but all of its refcounted fields would just be referenced.

view this post on Zulip Brendan Hansknecht (Nov 08 2024 at 20:37):

So a gigantic record could be problematic.

view this post on Zulip Jared Cone (Nov 08 2024 at 20:39):

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!

view this post on Zulip Brendan Hansknecht (Nov 08 2024 at 20:39):

Yep


Last updated: Jul 06 2025 at 12:14 UTC