While creating a client for a web API in C#, I ran into a problem regarding null
as a value where it would represent two different things:
- nothing, e.g. a
foo
may or may not have abar
- unknown: by default the API response only includes a subset of properties, you have to indicate which additional properties you want. So unknown means that the property was not requested from the API.
After some searching I found out about the Maybe (or Option) type, how it's used in functional languages and how it "solves" null dereferencing problems by forcing the user to think about the possible absence of a value. However, all of the resources I encountered talked about replacing null with Maybe. I did find some mentions of three-valued logic, but I don't fully understand it and most of the times its mentioning was in the context of "it's a bad thing".
I'm now wondering if it makes sense to have both the concept of null and Maybe, to represent unknown and nothing respectively. Is this the three-valued logic I read about, or does it have another name? Or is the intended way to nest a Maybe in a Maybe?
null
. It is a thoroughly broken idea. $\endgroup$M M x
andM x
should have the same semantics. $\endgroup$Maybe a
is the same as $a + 1$, semantically,Maybe Maybe a
is the same as $a+1+1$, and isomorphic toUserInput a
type from @Andej answer. You could define your own monad instance for this type too and therefore use different monad combinators. $\endgroup$M (M x)
andM x
should have the same semantics". TakeM = List
for instance: lists of lists are not the same thing as lists. WhenM
is a monad, there is a transformation (namely the monad multiplication) fromM (M x)
toM x
which explains the relationship between them, but they do not have "the same semantics". $\endgroup$