Option types are implemented in statically typed languages using dedicated types, such as Option in OCaml and Maybe in Haskell. The same approach can be used in Erlang:
-type option(T) :: {some, T} | none.
However, since Erlang allows union types to overlap, it might be advantageous to use a common atom for representing absence throughout the codebase; for example:
-type option(T) :: {some, T} | null. % Use `null` instead of `none`.
%% Only used at third-party boundaries; immediately converted to `option(T)`.
-type nullable(T) :: T | null. % T is never going to be `null`.
-type json_value() :: % Specification omitted.
-type json() ::
{some, json_value()} |
null | % Explicit JSON null.
undefined. % Missing input field or omitted output field.
Would you consider this idiomatic Erlang, or do you see any drawbacks to such design?
I’m not sure I understood you correctly. I’m not convinced Erlang would gain much from an Option type like OCaml’s. In OCaml, the compiler checks that you handle the absent case. Erlang is dynamically typed, so the same type would only be a convention, and idioms already exist for that. The JSON example is also confusing: null means both “absent” in option(T) and “explicit JSON null” in json().
I should have clarified that I statically type-check my code with strict checkers, namely a stricter variant of Eqwalizer that I’ve vibe-coded – and I can’t wait to switch to Etylizer – so a consistent type hierarchy matters in my case.
Regarding the JSON example: null means an absent value, whereas undefined means an absent field altogether. I should also have clarified that I’m serializing/deserializing JSON to records rather than maps, as seems to be common practice, so I’ve included a value to represent fields that must be ignored.
Would you mind expanding on the existing idioms you mentioned? My investigation suggests that the common idiom is essentially {some, T} | none or type_excluding_none() | none – which I intentionally avoid – just with different atoms, so I’m curious.
There isn’t a single meaning of ‘null’ appropriate for all cases
so programmrs should be encouraged to use problem-specific meaningful types.
The name ‘null’ is not commonly used in other FP languages.
ML uses (SOME x) or NONE.
Haskell uses (Just x) or Nothing.
Agda uses (just x) or nothing.
OCaml uses (Some x) or None.
F# agrees with OCaml (which is hardly surprising).
Elm agrees with Haskell.
Idris agrees with Haskell.
NONE is the opposite of SOME.
‘null’ is not the opposite of ‘some’.
Words like “missing” or “absent” or “not_found” would often be clearer.
I’m reminded of NULL in SQL. It’s used for
no such value could exist
no such value happens to exist
a value exists but we don’t know what it is
we don’t know whether a value exists or not.
The inventor of relational data bases, E. F. Codd, proposed in RMv2
“A-mark” the entity has the attribute but we do not know the value
“I-mark” the entity does not have the attribute
So it’s not at all clear that using a single ‘null’ for many situations
makes sense.
The classic example is ‘what is John’s phone number?’
John is a snail. He can’t own a phone.
John doesn’t have a phone.
John has an unlisted number.
Here you are…
For what it’s worth,
lists:keyfind/3 uses Tuple | false
lists:keysearch/3 uses {value,X} | false.
lists:keytake/3 uses {value,X,Y} | false.
dict:find/2 uses {ok,X} | error.
orddict:find/2 uses {ok,X} | error.
So right now there is no single Erlang idiom.
Let’s take one example. Imagine a function file:get_acl(Filename)
which is supposed to return some data structure representing the
access control list of a file. Possibilities are
there is no such file
the file exists but you aren’t allowed to ask about that
the file exists but it definitely doesn’t have an ACL
here’s the ACL you wanted.
Collapsing the first three possibilities onto a single ‘null’
makes it harder to respond appropriately.
It’s like the Boolean type: generally it’s a symptom that
maybe you should rethink the interface.
Your discussion about null matches what I was converging on: null is best kept at infrastructure boundaries – such as JSON – and immediately converted to an option that uses none.
Regarding your file:get_acl(Filename) example: yes, that’s usually handled by a different type, typically called result, with a success branch and an error branch, as it’s already informally done in Erlang: tipically {ok, Value} | {error, Error}, right?
I’m not as experienced in Erlang as you are, so I’m judging mostly from other languages. As I see it, the common idioms are the ones @nzok mentioned, plus {ok, X} | {error, Reason} and raising errors of various kinds.
As @nzok also noted, in OCaml None is an integral part of the Option type. Erlang has no equivalent: it’s all plain atoms, and how they relate is a matter of convention, not something the language enforces.
I’m not nearly as experienced in Erlang as I might seem, and like you, I’m mostly judging from other languages.
Yes, Erlang itself doesn’t treat atoms specially, but type checkers can. And without strict type checkers, I wouldn’t even be learning Erlang – maybe that makes me the oddball around here
not sure this is it but, if null stays in your json() type, a decoder that forgets to convert it to none wont get flagged unless dialyzer is actually running on that code.
Going back to your examples, I now notice that false is being used for types other than boolean, so I take it that overlapping union types are a common Erlang idiom. Your examples answered my question, albeit indirectly. Thank you again.