littleball-games / lb-f

A collection of functional programming functions
MIT License
0 stars 1 forks source link

Implement Maybe as classes plus helper functions #17

Closed skylize closed 6 years ago

skylize commented 6 years ago

Here is another take on Maybe. This is a first pass at trying to implement this as I described in my comments to #13.

Not a huge fan of using classes, but it's the only way I know of to define new verifiable types in vanilla js.

Note that the design abstracts the class interface away from the user. Typical usage should not require importing the classes themselves unless you need to do type checking (for example type signatures in TypeScript).

Thoughts?

skylize commented 6 years ago

One thing that's bothering me is whether this is the correct way to deal with null and undefined. It adds a lot of implicit behavior that simply doesn't exist in Haskell.

When using Haskell, the user would state in his function which values are valid for a Just and which should divert to Nothing and then Maybe is just an abstract type that can represent both cases.

But in trying to port it to JS we have added this extra magic of coercing nulls to Nothing without the user explicitly asking for it. And it kind of feels wrong.

skylize commented 6 years ago

Moving this commit to a different branch so I can use this branch to work against your implementation. If you want to view this code, you will find it at feature/maybe-class on my fork until sometime after we actually merge something for this feature.