AFAIK, roc does not yet have an option type. Something like
Option t : [ None, Some t ]
Plus functions for it.
I would like to submit code for one, implemented in roc itself. Would that be accepted?
we prefer Result a b
in practice
because we can create ad-hoc error types, that has little overhead
so you can actually say Err OutOfBounds
instead of getting a None
and having to look up in what cases that is actually created and what it means
No, actually it is not what I want. I want to represent a thing that is there or is not there... hence Option
, not Result
!
well we decided to go with Result
for the standard library. In your own programs you can do what you want of course
So how would someone use Result
to represent a thing that can be missing? Is there some way to make the b
in Result a b
to something like void
?
void
in the C sense I guess yeah. In rust/ML this is usually called "unit", a type with exactly one value
we can give it a custom name, like Result SomeType [ Missing ]
, or pick the generic Result SomeType {}
(in rust/ML Void
is the type with zero values, so no value of such a type can be created)
except with panics or diverging computation
infinite loop
yeah, sure. I was talking more C void, as I thought from your messages that you're not familiar with Rust, as you seem to not know what an Option is... sorry 'bout that
oh I know
and I know I've gone multiple levels of "go to definition" deep to figure out what None
actually means
there's now a PR for a FAQ entry about this!
Does the tutorial mention the optional record field feature? I didn't know about it, and didn't see it just now when searching for "option". Could maybe give a quick blurb for its syntax in this new faq entry?
not yet - I still need to write that part!
Are any functions for Result
actually part of roc? I know that we return results, but I didn't think we had any special methods for it.
there are a bunch
like map
, withDefault
etc
they are just implemented as ASTs, no lowlevels
Ok. I guess I never realized
@Jared Cone I looked as well and found it in the guid for developers coming from Elm. :wink:
Hmmm... not sure if this solves the original issue. Let's say I have a record, and in that record I want a field that represents a thing that may or may not have been set yet.
Customer : { name : Str, email ? Str }
Is there any way to match on that field? Or can you only extract it by passing it into a function like this?
emailHasBeenSet = \{name, email ? ""} -> if Str.isEmpty email then False else True
One nice thing about having an official Option type is it has a slew of helpful util functions like Option.map
. If every programmer has to instead write their own domain-specific option type for every field, do they also have to implement all those util functions?
Is there any way to match on that field?
not currently, but we've talked about supporting it; this isn't the first time an interest in that has come up!
Last updated: Jul 06 2025 at 12:14 UTC