Optional doc pattern #6
bcpeinhardt
started this conversation in
General
Replies: 1 comment 1 reply
-
Thank you for pointing this out Ben! I'm really curious to see common use cases :) I think both versions look great, to make it a bit more general it would be something like: fn from_option(option: Option(a), to_doc: a -> Document) -> Document {
case option {
Some(value) -> to_doc(value)
None -> doc.empty
}
} But I could also see people being annoyed with that and wanting something that doesn't default to the empty doc: fn from_option(option: Option(a), default: Document, to_doc: a -> Document) -> Document {
case option {
Some(value) -> to_doc(value)
None -> default
}
} Your example would look like this: fn pretty_import_alias(alias: Option(String)) {
doc.from_option(alias, doc.empty, fn(alias_string) {
" as " <> alias_string
})
} I don't know how I feel about adding this to the |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
One common pattern I'm running into a lot is defaulting to
doc.empty
in an option. For exampleI could of course also write this as
I don't necessarily have any suggestions here, I just wanted to let you know this was something that was popping up a lot :)
P.S. I've tried a few different versions of helper functions for this and they save a line or two but make the code less clear. Maybe you'll have better luck if you decide to explore this a bit :)
Beta Was this translation helpful? Give feedback.
All reactions