Stream: beginners

Topic: record builder syntax


view this post on Zulip Georges Boris (Dec 25 2024 at 18:04):

Hey folks - I'm trying to use the record builder syntax but I think there is something outdated between builtin docs and the tutorial.

Ref:
https://www.roc-lang.org/tutorial#record-builder
https://www.roc-lang.org/builtins/Task#batch

How would I use the record builder syntax these days with a Task?

My code as example using the basic-webserver@0.10 platform:

{ Task.ok <-
    a: Env.var "A",
    b: Env.var "B"
}

view this post on Zulip Sam Mohr (Dec 25 2024 at 18:30):

Oh, sorry, that's out of date yes

view this post on Zulip Sam Mohr (Dec 25 2024 at 18:33):

Record builders used to be built with applicative functors

view this post on Zulip Sam Mohr (Dec 25 2024 at 18:33):

Which was a complex tool for beginners to grok

view this post on Zulip Sam Mohr (Dec 25 2024 at 18:34):

So now we just accept any function that takes two things and combines them

view this post on Zulip Sam Mohr (Dec 25 2024 at 18:35):

So Task.batch is for the old style, and we don't have a function for the new style

view this post on Zulip Georges Boris (Dec 25 2024 at 18:35):

Oh wow! Yeah - I was still under the impression it was related to applicatives.

view this post on Zulip Anthony Bullard (Dec 25 2024 at 18:36):

The tutorial needs some love

view this post on Zulip Sam Mohr (Dec 25 2024 at 18:37):

combineTasks : Task a err, Task b err, (a, b -> c) -> Task c err
combineTasks = \leftTask, rightTask, combiner ->
    left = leftTask!
    right = rightTask!

    Task.ok (combiner left right)

view this post on Zulip Sam Mohr (Dec 25 2024 at 18:37):

This function should be a userspace record builder combiner

view this post on Zulip Sam Mohr (Dec 25 2024 at 18:38):

With Purity Inference, you shouldn't really need this

view this post on Zulip Sam Mohr (Dec 25 2024 at 18:41):

But you could do this:

combine! : ({} => Result a err), ({} => Result b err), (a, b -> c) => Result c err
combine! = \leftGen, rightGen, combiner ->
    left = try leftGen! {}
    right = try rightGen! {}

    Ok (combiner left right)

view this post on Zulip Georges Boris (Dec 25 2024 at 18:45):

Why does it need to be a userspace function? Shouldn't it be part of the Task module?

view this post on Zulip Sam Mohr (Dec 25 2024 at 18:46):

It should, but Task is going the way of the dodo pretty soon

view this post on Zulip Georges Boris (Dec 25 2024 at 18:46):

Maybe I'm misinterpreting what userspace means :grinning_face_with_smiling_eyes:

view this post on Zulip Sam Mohr (Dec 25 2024 at 18:46):

I guess I could add that just as a stopgap

view this post on Zulip Georges Boris (Dec 25 2024 at 18:46):

Ohhh right - with the purity inference thing

view this post on Zulip Sam Mohr (Dec 25 2024 at 18:46):

Not sure what the right name is.

view this post on Zulip Sam Mohr (Dec 25 2024 at 18:46):

view this post on Zulip Sam Mohr (Dec 25 2024 at 18:46):

What would you prefer?

view this post on Zulip Georges Boris (Dec 25 2024 at 19:08):

combine makes sense to me! I like that it seems like a reusable name for the pattern to be used in different modules.

view this post on Zulip Sam Mohr (Dec 25 2024 at 19:09):

I'll make a PR, then

view this post on Zulip Sam Mohr (Dec 25 2024 at 20:01):

@Georges Boris could you look at https://github.com/roc-lang/roc/pull/7413 if you get a chance and tell me if the docs make sense to you?

view this post on Zulip Sam Mohr (Dec 25 2024 at 20:02):

It's pretty simple

view this post on Zulip Georges Boris (Dec 25 2024 at 20:29):

Looks good to me!

I wonder if it wouldn't be better to remove the previous batch example as a whole since that code doesn't compile anymore.

view this post on Zulip Sam Mohr (Dec 25 2024 at 20:29):

Yeah, probably the right move

view this post on Zulip Sam Mohr (Dec 25 2024 at 20:30):

done


Last updated: Jul 05 2025 at 12:14 UTC