Result(a, Nil) vs Option(a) #1265
-
I was reading through the Result page of The Gleam Book and found Given that, my question is: Are there cases where one should use |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
I also found that the I suppose this is for convenience to allow binding returned values with |
Beta Was this translation helpful? Give feedback.
-
Hello! Good question, thank you. Result is to be used for operations that may not succeed, such as attempting to retrieve an entry from a data structure. Other languages use a mix of option and result for this scenario, but Gleam always uses result. This consistency means we have to do less converting between the two types and our special Later we will have better documentation for this, what we currently have is a bit basic while the core language and tooling is being worked on. |
Beta Was this translation helpful? Give feedback.
-
This strikes me as pretty unintuitive: both Haskell and Rust use their
equivalent of Option (Maybe and Option, respectively) when retrieving
values from a list or map. Having Gleam be consistent with other strongly
typed functional languages would reduce cognitive friction for users coming
from those languages. The way Rust's Result or Haskell's Either are used is
when there's "something else" involved, usually an error or separate
branch. I'm also personally somewhat uncomfortable with the use of `Nil` in
this context because of typing implications: is `Nil` an inhabitant of
every type (like `undefined` in Haskell)? Is `Result(a, b)` secretly
desugared into `Result(a, b | Nil)`? Neither seem terribly sound or, again,
intuitive. Lastly, for whatever it's worth, `Result(a, Nil)` is isomorphic
to `Option(a)` from a category theory perspective--it's trivial to
implement functions that translate between them.
…On Tue, Sep 14, 2021 at 9:19 AM Louis Pilfold ***@***.***> wrote:
I wouldn't consider looking for an item in a list and not finding it a
"failure". There is either Some element in the list that matches a
condition or None.
These are the semantics in Gleam. "Did you manage to get the Nth value
from this array?" "No, I was unable".
It's fine either way IMO, though making Result(a, Nil) compete with
Option(a) creates redundancy.
It is the inverse! In Gleam Result is *always* used. To use result
sometimes and to use option other times produces redundancy. Gleam is more
consistent than F# in this way.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#1265 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAOSJHTPGOAVYCXDEJ4DBX3UB5KYHANCNFSM5D4LP5TQ>
.
--
.
|
Beta Was this translation helpful? Give feedback.
Hello! Good question, thank you.
Result is to be used for operations that may not succeed, such as attempting to retrieve an entry from a data structure. Other languages use a mix of option and result for this scenario, but Gleam always uses result. This consistency means we have to do less converting between the two types and our special
try
syntax works with all fallible functions.Later we will have better documentation for this, what we currently have is a bit basic while the core language and tooling is being worked on.