Stream: beginners

Topic: Help with stringify and result mapping


view this post on Zulip Tom (Nov 18 2023 at 22:16):

Hi, I'm trying to convert some example code that munges on a data model with optionals and errors. I may have skimmed over it in the tutorial, and maybe it relates to encoders, but how do I just write structured data to stdout in an easy fashion? Example of what I'd like to do here:

https://github.com/contextfreecode/rocdemo/blob/0e7ee7c346d220c7ac29bb2c90353b85521d9ada/main.roc#L55

Also, I made some Result mapping helpers at the bottom of this file. Is anything built-in that I've missed? Using when for everything is bulky. And I see some related looking things on cli Task (like map and mapErr), but I haven't simulated using Task for my simulated network reading, and I'm not sure if that would be the right things anyway.

Thanks!

view this post on Zulip Tom (Nov 18 2023 at 22:22):

Also, since I'm modeling data with optionals, I couldn't use optional record fields. So I used Err {} for none. I tried saying null = Err {}, but it seemed to fail at row polymorphism. Any way to define a row polymorphic constant like this?

view this post on Zulip Brendan Hansknecht (Nov 18 2023 at 22:35):

but how do I just write structured data to stdout in an easy fashion?

We have a partially implemented but not complete inspect ability that plans to be autoderived that would enable this. Until then, manually writing a function or using a encoder is the best option.

Is anything built-in that I've missed?

Result as map, mapErr, try, and onErr. With backpassing, that tends to avoid both deep nesting and lots of when .. is expressions. https://www.roc-lang.org/builtins/Result

Any way to define a row polymorphic constant like this?

I am pretty the answer is that there is no way to do that. It would have to be a function that returns the value to get the results you expect.

view this post on Zulip Tom (Nov 18 2023 at 22:39):

Wow, I managed to overlook the Result section there. Thanks for that and the other info!

view this post on Zulip Luke Boswell (Nov 18 2023 at 22:48):

I thought Inspect works ok, is there anything still outstanding with that? Here is the example https://github.com/roc-lang/roc/blob/main/examples/inspect-logging.roc

view this post on Zulip Luke Boswell (Nov 18 2023 at 22:48):

$ roc run examples/inspect-logging.roc
{people: [{firstName: "John", lastName: "Smith", age: 27, hasBeard: true, favoriteColor: Blue}, {firstName: "Debby", lastName: "Johnson", age: 47, hasBeard: false, favoriteColor: Green}, {firstName: "Jane", lastName: "Doe", age: 33, hasBeard: false, favoriteColor: (RGB (255, 255, 0))}], friends: [{2}, {2}, {0, 1}]}

view this post on Zulip Luke Boswell (Nov 18 2023 at 22:49):

Maybe it is the autoderive for Inspect on Roc types that doesn't work yet, so you have to supply a custom formatter, e.g. in this case LogFormatter

view this post on Zulip Brendan Hansknecht (Nov 18 2023 at 22:53):

yeah, no autoderive

view this post on Zulip Brendan Hansknecht (Nov 18 2023 at 22:53):

Manual definitions: https://github.com/roc-lang/roc/blob/main/examples/Community.roc#L94

view this post on Zulip Luke Boswell (Nov 18 2023 at 22:55):

Ahk, thanks for looking into that

view this post on Zulip Richard Feldman (Nov 18 2023 at 23:14):

yeah I have a WIP branch that I paused on to work on new website stuff; I plan to get back to it once the new website is out!

view this post on Zulip Tom (Nov 18 2023 at 23:15):

Thanks for the examples!

view this post on Zulip Tom (Nov 18 2023 at 23:30):

Any chance that automating inspect will also apply to tagged types including Result?

view this post on Zulip Brendan Hansknecht (Nov 18 2023 at 23:51):

inspect should work on all types automatically if they aren't opaque


Last updated: Jul 05 2025 at 12:14 UTC