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. – Andrej Bauer Dec 30 '16 at 14:16M M x
andM x
should have the same semantics. – Eric Lippert Dec 30 '16 at 15:19Maybe 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. – Euge Dec 30 '16 at 16:04M (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". – Andrej Bauer Dec 30 '16 at 20:25Maybe<boolean>
as opposed to plainboolean
(not being language-specific here of course). – einpoklum Dec 30 '16 at 22:19m ~ Maybe
, there is some use in having a double wrapping. Suppose you use a map as a cache, caching results of calls to a function of typea -> Maybe b
. ThenNothing
means "this result is not cached", andJust Nothing
means "This result has been cached, and the answer isNothing
". But of course I do agree that usingMaybe (Maybe a)
for this particular problem is no good. – amalloy Dec 31 '16 at 06:24