Stream: contributing

Topic: Alignment of Roc types


view this post on Zulip Luke Boswell (Jul 01 2024 at 03:47):

G'day all, hand rolling types can be tricky sometimes. So I made a simple script using roc glue which prints out the fields in a struct so you can know what order they should go in.

For example this type isn't necessarily obvious how it should be represented in C.

Command : {
    program : Str,
    args : List Str, # [arg0, arg1, arg2, arg3, ...]
    envs : List Str, # [key0, value0, key1, value1, key2, value2, ...]
    clearEnvs : Bool,
}

So using this script in roc-glue-code-gen you can do;

$ roc glue examples/alignment.roc examples/test/ examples/platform.roc
🎉 Generated type declarations in:

    examples/test/
$ cat examples/test/types.txt
{id: (@TypeId 2), name: "args"}
{id: (@TypeId 2), name: "envs"}
{id: (@TypeId 1), name: "program"}
{id: (@TypeId 3), name: "clearEnvs"}%

Which translates in Rust to

#[repr(C)]
#[derive(Debug)]
pub struct Command {
    pub args: RocList<RocStr>,
    pub envs: RocList<RocStr>,
    pub program: RocStr,
    pub clearEnvs: bool,
}

view this post on Zulip Luke Boswell (Jul 01 2024 at 03:57):

I should probably put a roc CLI app at the top level of that repo to make this kind of thing easier to use... :idea: not a priority RN, I'd like to finish a few other things

view this post on Zulip tarkah (Jul 06 2024 at 04:49):

I wish I saw this sooner! I'll be sure to use this going forward as I'm pretty much 100% hand rolling the glue now for my roc-iced platform


Last updated: Jul 06 2025 at 12:14 UTC