Skip to content
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

Maybe from null map error #208

Open
pedroth opened this issue Mar 27, 2019 · 5 comments
Open

Maybe from null map error #208

pedroth opened this issue Mar 27, 2019 · 5 comments
Labels

Comments

@pedroth
Copy link

pedroth commented Mar 27, 2019

I found that when writing the following code:

Maybe.fromNull(1).map(x => undefined).orSome(1)

I receive the error: Can not create Some with illegal value: undefined.

I guess the issue is on the map function, that composes a function with Maybe.of instead of Maybe.fromNull

Thanks

@joshlgrossman
Copy link

You can’t map to an undefined/null value. You should flatMap to a new Maybe.

@pedroth
Copy link
Author

pedroth commented May 18, 2019

Thanks,
I ended up using the flatMap.

@ulfryk
Copy link
Member

ulfryk commented May 23, 2019

@pedroth - sorry for long time to response..

See #53
especially #53 (comment)

@turingcreep
Copy link

[Newbie alert] - Hi, I am new to functional programming, so i may be completely off the mark. Using this as a reference https://jrsinclair.com/articles/2016/marvellously-mysterious-javascript-maybe-monad/

console.log(
  Maybe.fromNull({
    current: 1
  }).map(x => x.current)
); // this works
console.log(
  Maybe.fromNull({
    current: null
  }).map(x => x.current)
); // This doesn't work
// Throws the error  **Can not create Some with illegal value: null.**

If i understand correctly, the maybe monad is supposed to be able to handle all the null and undefined stuff. Thus,

console.log(
  Maybe.fromNull({
    current: null
  }).map(x => x.current)
);  // Should return a None and the output should be 
// init {isValue: false, val: null, @@type: "monet.js/Maybe"}

should instead return me a None or Nothing monad. If we do it like this, we don't need to do checks like if (a && a.b && a.b.c) fn(a.b.c); Instead can implement this as Maybe.of(a).map(x => x.b).map(x => x.c).map(fn). It wouldn't matter if either b or c is null; If they are, then the final monad will also be Nothing.

Please correct me if i'm wrong; If not, let me know if i can submit a PR with the desired behavior.

Regards.

@ulfryk
Copy link
Member

ulfryk commented Apr 28, 2020

@turingcreep you are definitely on a good path thinking that Maybe is able to handle all the null and undefined stuff. But in Monet library it does it in a special manner - it requires you to know the types you use and handle them by yourself. It gives you the type and runtime error guards.

So in case of Monet implementation of Maybe you need to do:

Maybe.fromNull({
  current: AnythingThatMayBeNullableOrUndefined
}).flatMap(x => Maybe.formNull(x.current))

See my comment above your question - it links to a discussion that describes this decision in detail :)


side note: you can Some({ current: … }) as you know object is not null ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants