-
Notifications
You must be signed in to change notification settings - Fork 0
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
Should mapping to None be allowed? #76
Comments
.map
ping to None be allowed?
This is a good question. As far as monadic laws are concerned, I think it only matters that it returns either a What about this case: obj = { a: 'hi' }
Maybe(obj)
.map(prop('a'))
.map(prop('b'))
.map(prop('c')) I guess I would expect the One potential type-level option could also be to refuse to accept |
Haskell it's sort of a non-issue as there isn't really a concept of null in the language. ( Optional<String> opt = Optional.of("Hello World!");
System.out.print(opt.map((String x) -> null)); // Output: "Optional.empty" So there's definitely precedent for this behavior (and I guess that's probably a good sign that there's not a "maybe spec" like there is a "promise spec"). Short term, I definitely think disallowing Long term, it might be worth a little more thought. I'm still not sure which approach I think is better. If we did go the other way, I suppose your example would have to be written like: obj = { a: 'hi' }
const maybeProp = (prop) => obj => maybe(obj.prop);
Maybe(obj)
.flatMap(maybeProp('a'))
.flatMap(maybeProp('b'))
.flatMap(maybeProp('c')) That's not wonderful (though at least the types would guide the user toward that solution). So I'm leaning towards keeping it how it is, but maybe I'll dig through fp-ts and see if I can find a rationale for why they do |
Is it intentional design that we can transform a
Some
to aNone
with a map function? For example:Calling this function with
log(some(5))
will log both "Got 5" and "Got Nothing". Since the map function doesn't return anything, it becomes aNone
and theorElse
case runs. (This can be fixed with either.tap
or.caseOf
)I can see some cases where you'd want to map from
Some
toNone
, but I think it's also unexpected in a lot of cases, like above. I don't know if this behavior is specified by any standard. I compared tofp-ts
and their Optional will not map to aNone
in this case, it'll becomeMaybe<undefined>
.The text was updated successfully, but these errors were encountered: