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:
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!
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?
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.
Wow, I managed to overlook the Result section there. Thanks for that and the other info!
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
$ 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}]}
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
yeah, no autoderive
Manual definitions: https://github.com/roc-lang/roc/blob/main/examples/Community.roc#L94
Ahk, thanks for looking into that
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!
Thanks for the examples!
Any chance that automating inspect will also apply to tagged types including Result?
inspect should work on all types automatically if they aren't opaque
Last updated: Jul 05 2025 at 12:14 UTC