-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
map is able to create Just(null) #15
Comments
That is the intended behaviour of map. It is up to the consumer to decide when a field should be checked for nullabilitly. Regarding your example, here is how it should be done: import { nullable, get } from 'pratica';
const data = { foo: null };
const maybe = nullable(data).chain(get('foo')); |
You might want to investigate parametric polymorphism to learn why this behavior is important. |
Ok, thanks for the answers! They made me understand FP a little bit better. :-) When I was posting this, I was pretty sure that it is a bug, but now I can see a lot of similar posts in the discussion of other libraries. Looks like that the main battle was over 5 years ago. :-) Perhaps my main misconception about Maybe is that it is a single type and In practice, it means that if we want a null-free behavior, we should never use const maybe = nullable(data).chain(({foo})=>nullable(foo)); Fortunately, your nice If you ever consider extending your documentation with some "best practices"/code examples, I believe it should have a section discussing this issue. |
Some best practice docs would be good, I agree. |
Ah, the thread that beat this into my brain! It's been a while since I reread that. |
It looks like that Maybe.map is not working as it should - if a function you pass to map returns null, it creates Just(null) instead of Nothing!
Expected output: Nothing
Actual output: Just(null)
I have taken a look through the source code and here is a problem
pratica/src/maybe.ts
Line 21 in 50379a4
Every time when the map is called for a Maybe which contains a value, it always assumes that the result should be wrapped to Just as well. However, in the situations when it returns null, you would get Just(null) which is completely wrong.
To fix it, you should use nullable instead of Just:
Also, I would suggest considering adding a constructor to Just and Nothing which would throw an exception when an incorrect value is passed there.
The text was updated successfully, but these errors were encountered: