#Option vs Result

1 messages · Page 1 of 1 (latest)

edgy relic
#

A lot of the std lib uses Result(a, Nil) in places (for example Dict.get) where other languages would use Maybe. I had always thought of the rule of thumb being

  • representing the presence or absence of something, use Maybe. (e.g. the list may or may not have a first item, a dict/map may or may not have some value for a given key)
  • representing something that could error or otherwise fail, use Result, (e.g making an HTTP request, writing a file)

This doesn't really seem to apply to what I see in the std lib. What's the criteria you use to decide when to use o Option vs Result(a, Nil) ? Is there a similar idiomatic rule of thumb?

oblique tiger
nimble sphinx
#

from https://tour.gleam.run/standard-library/option-module/:

Some languages have functions return an option when there is no extra error detail to give, but Gleam always uses result. This makes all fallible functions consistent and removes any boilerplate that would be required when mixing functions that use each type.