Stream: contributing

Topic: Option type?


view this post on Zulip Matthias Beyer (Mar 13 2022 at 12:32):

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?

view this post on Zulip Folkert de Vries (Mar 13 2022 at 12:34):

we prefer Result a b in practice

view this post on Zulip Folkert de Vries (Mar 13 2022 at 12:34):

because we can create ad-hoc error types, that has little overhead

view this post on Zulip Folkert de Vries (Mar 13 2022 at 12:34):

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

view this post on Zulip Matthias Beyer (Mar 13 2022 at 12:35):

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!

view this post on Zulip Folkert de Vries (Mar 13 2022 at 12:41):

well we decided to go with Result for the standard library. In your own programs you can do what you want of course

view this post on Zulip Matthias Beyer (Mar 13 2022 at 12:43):

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?

view this post on Zulip Folkert de Vries (Mar 13 2022 at 12:46):

void in the C sense I guess yeah. In rust/ML this is usually called "unit", a type with exactly one value

view this post on Zulip Folkert de Vries (Mar 13 2022 at 12:46):

we can give it a custom name, like Result SomeType [ Missing ], or pick the generic Result SomeType {}

view this post on Zulip Folkert de Vries (Mar 13 2022 at 12:47):

(in rust/ML Void is the type with zero values, so no value of such a type can be created)

view this post on Zulip Folkert de Vries (Mar 13 2022 at 12:47):

except with panics or diverging computation

view this post on Zulip Folkert de Vries (Mar 13 2022 at 12:47):

infinite loop

view this post on Zulip Matthias Beyer (Mar 13 2022 at 12:48):

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

view this post on Zulip Folkert de Vries (Mar 13 2022 at 12:49):

oh I know

view this post on Zulip Folkert de Vries (Mar 13 2022 at 12:49):

and I know I've gone multiple levels of "go to definition" deep to figure out what None actually means

view this post on Zulip Richard Feldman (Mar 13 2022 at 14:21):

there's now a PR for a FAQ entry about this!

view this post on Zulip Jared Cone (Mar 13 2022 at 14:43):

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?

view this post on Zulip Richard Feldman (Mar 13 2022 at 15:44):

not yet - I still need to write that part!

view this post on Zulip Brendan Hansknecht (Mar 13 2022 at 17:39):

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.

view this post on Zulip Folkert de Vries (Mar 13 2022 at 17:42):

there are a bunch

view this post on Zulip Folkert de Vries (Mar 13 2022 at 17:42):

like map, withDefault etc

view this post on Zulip Folkert de Vries (Mar 13 2022 at 17:43):

they are just implemented as ASTs, no lowlevels

view this post on Zulip Brendan Hansknecht (Mar 13 2022 at 17:43):

Ok. I guess I never realized

view this post on Zulip Johannes Maas (Mar 14 2022 at 10:22):

@Jared Cone I looked as well and found it in the guid for developers coming from Elm. :wink:

view this post on Zulip Jared Cone (Mar 14 2022 at 16:29):

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?

view this post on Zulip Richard Feldman (Mar 14 2022 at 16:46):

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