That’s a question for the PEP authors, as they are the ones who have rejected a “maybe” builtin.
But some thoughts come to mind.
First, we need to distinguish between a maybe
builtin type and a “maybe” builtin operator.
You seem to be asking about a maybe operator:
data = maybe data else []
This is a binary operator (it takes two arguments, data
and []
) but it is written using two keywords, one prefix maybe data
and one infix data else []
. That seems extravagently verbose, possibly even wasteful.
Python code often reads like executable pseudocode, but not like natural language. Using two keywords for a binary operator is something I would expect from a language like Hypertalk, where we might write things like:
add 1 to x
put 2*x into y
Also, as a native English speaker, the else
part doesn’t feel right to me. maybe data or []
reads better, but of course that leads to precedence issues with the binary or
operator.
Most other languages with a null-coalescing operator seem to be converging on using question marks. It seems wasteful to use two key words to accomplish what a single infix symbol can do:
data ?? []
maybe data else []
And how would you chain these expressions?
data ?? info ?? []
maybe data else maybe info else []
maybe maybe data else info else []
And what about the maybe dot operator?
a?.b(c)
Coming back to the PEP, I’m not one of the authors but my reading of that section is that pymaybe
has a function maybe()
that returns either a Something
instance or a Nothing
instance. The PEP suggests that instead of using a function and two types, they could use a single builtin maybe
type to work the same way.
The PEP already gives the disadvantages of this:
I expect that getting this sort of proxying right is much harder than giving the interpreter a binary infix operator ??
and supporting chaining.
The Haskell docs for maybe
might as well be written in Martian. But if I am interpreting them correctly, it seems that maybe
is used for falsey/truthy checks, with the added twist that you can define any values you like as falsey. Using the word maybe
may mislead people into thinking we have Haskell’s maybe
functor when we don’t.