isaacsanders

isaacsanders

Names for Monadic Modalities

I find myself coming back to a handful of types that I have recently made modules for, and I believe that others have made libraries to work with, and I wanted to see if we could have a discussion about the appropriate names for some types.

The types I keep on encountering and the names that I have chosen for them:

@typedoc """
A nullable value.
""" 
@type optional(a) :: a | nil when a: term()

@typedoc """
The result of an action that can unambiguously fail, e.g. `Map.fetch/2`
"""
@type maybe(a) :: {:ok, a} | :error when a: term()

@typedoc """
The result of an action that fails multi-modally and may be handled per failure mode.
"""
@type either(x, a) :: {:ok, a} | {:error, x} when a: term(), x: term()

What do y’all think?

Most Liked

jeremyjh

jeremyjh

I don’t think maybe or either are the most intuitive names for non-Haskellers. I call your either one result in my projects.

@type result(x, a) :: {:ok, a} | {:error, x} when a: term(), x: term()

By the way, Dialyzer doesn’t unify type variables so that signature is functionally no different from

@type result(x, a) :: {:ok, a} | {:error, a} when a: term()

Or the more idiomatic

@type result(x, a) :: {:ok, term()} | {:error, term()}

Exceptional calls it a TaggedStatus but doesn’t define a @type for it that I could find.

Qqwy

Qqwy

TypeCheck Core Team

I don’t think that these newly defined types make a lot of sense, because it is an extra layer of indirection a newcomer has to get used to when encountering either(x, y) in the return type of a function.

I do think that it would make sense if we somehow standardize the naming for thesee things, however. There are a couple of libraries that work on structures of the shape {:ok, a} | {:error, b} (and the less common {:ok, a} | :error), but there is no real consensus on the naming of these structures.
I think that names like ‘Result Tuple’, ‘OK Tuple’, ‘Error Tuple’, ‘Success Tuple’ are frequently used (by e.g. ok_jose, if_ok, ok, fun_land) but they are not the only ones. I know that exceptional uses ‘tagged status’ to indicate these structures, and maybe there are even others.

For instance, I’m not sure if Witchcraft also defines Maybe/Result monads based on tuples of this specific format, or not.

OvermindDL1

OvermindDL1

It defines custom structures. It’s a library of design, not of efficiency. :slight_smile:
(Well not witchcraft itself as it’s a library to build such things, but the ones built on it is.)

Where Next?

Popular in Discussions Top

AstonJ
Are there any Elixir or Erlang libraries that help with this? I’ve been thinking how streaming services like twitch have exploded recentl...
New
AstonJ
I’ve just started the Phoenix part of the utterly brilliant online course by @pragdave. On generating the Phoenix app he uses the --no-ec...
New
tmbb
This is a post to discuss the new Phoenix LiveView functionality. From Chris’s talk, it appears that they generate all HTML on the serve...
342 18634 126
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
thojanssens1
It would be nice to be able to define a redirect from one route to another from the router.ex file. E.g.: redirect "/", UserController, ...
New
nunobernardes99
Hi there Elixir friends :vulcan_salute: In a recent task I was on, I needed to check in two dates which of them is the maximum and which...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54260 488
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement