Maybe.of(null) should return Nothing() and not Just(null)
See original GitHub issuevar Maybe = require('data.maybe')
Maybe.of(user); //if the user is Null,
… then it should return Nothing() object and not Just object! I know there is Maybe.fromNullable() but that defeats the purpose of having “of” creating proper Nothing or Just object.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Elm Maybe - Dealing with null/Nothing
head returns the first item of a List. The reason it returns a Maybe is because the List might be empty. In this...
Read more >Just Don't Return null!
On my article Avoid Explicit null Checks i talked about alternative ways to handle a null value that don't involve explicitly doing null...
Read more >3 Clever Ways to Return Empty Value Instead of Null From ...
It is best practice to return empty values rather than null ones. Especially when you return a collection, enumerable, or an object, you...
Read more >Should functions return null or an empty object?
An empty object implies data has been returned, whereas returning null clearly indicates that nothing has been returned. Additionally, returning ...
Read more >Is it better to return NULL or empty values from functions ...
An empty object implies data has been returned, whereas returning null clearly indicates that nothing has been returned. Additionally, returning ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
So,
Maybe.of
is just an implementation of the Applicative interface, and one of the requirements of that interface is that the value should be stored as-is, and no parts of it should be checked. That is, for that interface, if you havea.of(b)
anda.of(c)
, then you should always get a value of the same type, containing exactlyb
andc
.As for the naming choice here, Ramda seems to have
Maybe
as a function that constructs values of type Maybe. But in Folktale,Maybe
is just a namespace. HavingMaybe
as a function-namespace then wouldn’t work — we want people to be able to just pick pieces of the library they want to use, rather than needing to load everything.You may alias the function as you wish, however. This would work:
Concerning Maybe and null/undefined serving similar purposes, that’s not quite so.
In JavaScript, and in many other languages,
null
is a value that inhabits any type, so it’s a global “lack of value”. So, in this sense, if you have a functionf : int → int
and a functiong : string → string
, then bothf(null)
andg(null)
are valid applications of those functions, as aref(1)
andg("foo")
, and your code must deal with the possibility of those values not being present.On the other hand, if you have a function
f : int? → int?
and a functiong : string? → string?
, thenf(null)
andg(null)
aren’t valid, becausenull
isn’t part ofint?
orstring?
. You have to, instead, construct a new value of typeint?
, thusf(Nothing())
andg(Nothing())
. Likewise,f(1)
andg("foo")
are invalid, because they’re typeint/string
, notint?/string?
, so you need to wrap them as suchf(Just(1))
andg(Just("foo"))
. Your functions then have to always unwrap the value.In essence,
null
gives you a global “lack of value”, since it’s one value that inhabits every type. It’s supposed to be impossible to distinguish “null for int” and “null for string”. They’re both justnull
. TheMaybe
gives you an opportunity of having a per-type “lack of value”, soNothing() for int
andNothing() for string
are very different values, and they’re not interchangeable.Their designs and goals are different. (Unfortunately JavaScript can’t enforce that, but see Swift for an interesting take on this topic :3)
As for why the name
fromNullable
, it’s named after the “global lack of value” → “per-type lack of value” conversion (it has typeforall a, given a ≠ null and a ≠ undefined. a or null or undefined →Maybe a
. Compare withof : forall a. a → Maybe a
to see the diference). I particularly like names that more explicitly describes operations, and I dislike abbreviations, so that’s the approach Folktale takes on naming in particular, henceMaybe.fromNullable(a)
instead of just something likeMaybe.from(a)
ormaybe(a)
.This would break the monad laws.
According to them,
of
should not change the value, it should just put it in a different context.