Skip to content

Should mapping to None be allowed? #76

Description

@Retsam

Is it intentional design that we can transform a Some to a None with a map function? For example:

function log(maybe: Maybe<unknown>) {
    maybe
        .map(x => console.log(`Got ${x}`))
        .orElse(x => console.log(`Got nothing`))
}

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 a None and the orElse case runs. (This can be fixed with either .tap or .caseOf)

I can see some cases where you'd want to map from Some to None, 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 to fp-ts and their Optional will not map to a None in this case, it'll become Maybe<undefined>.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions