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

vans163
So useless benchmarks aside, Its possible to write a webserver that can serve 300k requests per second (perhaps more with optimizations)....
New
owaisqayum
I have a sample string sentence = "Hello, world ... 123 *** ^%&*())^% %%:>" From this string, I want to only keep the integers, ...
New
New
MarioFlach
Hello, I want to share a project I’ve been working on for a while: https://github.com/almightycouch/gitgud Background Some time ago I ...
New
Fl4m3Ph03n1x
Background This question comes mainly from my ignorance. Today is Black Friday, one of my favorite days of the year to buy books. One boo...
New
WolfDan
After doing a port from a c++ library to my project in phoenix I’ve seen that I need a faster way to run this algorithm and I found this ...
New
chuck
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
PragTob
Hey everyone, this has been brewing in my head some time and it came up again while reading Adopting Elixir. GenServers, supervisors et...
New
acrolink
How does the two languages compare when it comes to server side application development? Any experiences or ideas? Thank you.
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New

We're in Beta

About us Mission Statement