Is it just me or is there no convenience functions to create Maybe
or Either
from something potentially nullable?
Can you give any example of that you are doing?
Basically something like this:
(let [result (something-that-might-return-null ...)]
(if result
(just result)
(nothing)))
And that's quite verbose and I was wondering if I was missing a convenience function that could encapsulate that
(and something analogous for Either
where you would specify the Left
value if the result was nil
)
I agree for Maybe but not for Either
That behavior for either is too much opinionated
Well, I partly agree that Either is more semantically rich than Maybe
here and there's more reason to handle both Right
and Left
by hand
the cats.monad.exception already provides a "Either" analogous abstraction just for succes and failure and also provides a try-on
macro that allows easy wrap any result in a proper type
(exc/try-on (somethird-party-fn-that-raises-exceptions))
note that nil
already participates in the Maybe abstraction, is equivalent to Nothing
Right!
I forgot about it...
for use that you just need to (require '[cats.builtin])
And then I don't have to wrap it by hand every time? I see.
hmm
no, I think we should create the analogous try-on macro for maybe for that...
the nil is treated as nothing, but any other value is not treated as Just...
I see, I'll just keep doing the manual wrapping for now then
Nice!
We surelly add the helper for the next version
we could call it ensure-maybe
, its behavior will be similar to core's ensure-reduced
Since you already have maybe/from-maybe
then maybe to-maybe
or from-nullable
could be a good choice as well?
I dig to-maybe
👍, happy to merge a patch with these additions
I've opened an issue to not forget about it https://github.com/funcool/cats/issues/133
Hmm
to-maybe
is ok, but I'm will be more happy with something like try-on
naming
to maybe is just a conversion, but I think it can be a macro
just for avoid additional call stack frame
or provide the two variants, macro and function
that is possible in cljs...