Stream: beginners

Topic: call roc or call platform?


view this post on Zulip Arya Elfren (Aug 31 2022 at 20:15):

The default currently is that a platform calls roc main (I forget the exported function name) and then roc calls the platform for memory and other functions.

Is it possible to export multiple roc functions? So you just have some core business logic in roc but the platform does the main loop and branching?

Also, can you implement custom platform data structures that roc can manipulate?

view this post on Zulip Brendan Hansknecht (Aug 31 2022 at 20:21):

I would say yes and yes, but there are still caveats to both answers

view this post on Zulip Brendan Hansknecht (Aug 31 2022 at 20:22):

Roc can export multiple functions by returning them in a struct. Then the platform can technically call directly into those functions without ever calling main.

view this post on Zulip Brendan Hansknecht (Aug 31 2022 at 20:23):

The issue is that without calling main first, you may end up trying to call into a closure without the correct captured args.

view this post on Zulip Brendan Hansknecht (Aug 31 2022 at 20:24):

So generally I would change the roc main function to be called init. Then call if to get closure related information, but that isn't strictly necessary in many cases

view this post on Zulip Brendan Hansknecht (Aug 31 2022 at 20:24):

My example with SDL does this.

view this post on Zulip Brendan Hansknecht (Aug 31 2022 at 20:25):

https://github.com/bhansconnect/roc-ecs

view this post on Zulip Brendan Hansknecht (Aug 31 2022 at 20:26):

As for manipulation of host data structure. Roc has effects. As long as the host exposes and effect that does the manipulation, roc will be able to call it. Thus roc can do any manipulation the host could do.

view this post on Zulip Folkert de Vries (Aug 31 2022 at 20:41):

we plan to make this smoother by generating the FFI code. It already works (really well) for anything but functions

view this post on Zulip Arya Elfren (Aug 31 2022 at 20:44):

Yea, so I can have a platform as a cli wrapper and the roc is just a "library" that it calls into? So roc library is easier to write complex and memory safe stuff for but it doesn't do cli parsing etc.

view this post on Zulip Arya Elfren (Aug 31 2022 at 20:44):

How can I test the FFI generation?

view this post on Zulip Folkert de Vries (Aug 31 2022 at 20:46):

> target/debug/roc glue
error: The following required arguments were not provided:
    <ROC_FILE>
    <GLUE_FILE>

USAGE:
    roc glue <ROC_FILE> <GLUE_FILE>

this only works for rust platforms (for now) actually

view this post on Zulip Folkert de Vries (Aug 31 2022 at 20:47):

so glue file would be output.rs or something

view this post on Zulip Folkert de Vries (Aug 31 2022 at 20:47):

and the roc file is the platform's main.roc I believe

view this post on Zulip Arya Elfren (Aug 31 2022 at 21:02):

Ok, thanks!


Last updated: Jul 06 2025 at 12:14 UTC