Provide a way to get any values that exist in an Option.Option _
or none at all
#18
Labels
enhancement
New feature or request
Option.Option _
or none at all
#18
The idea
I was talking to @gabejohnson the other day about using
purescript-option
to solve a problem. It came up that it'd be ideal if we could take anOption.Option _
, and convert it toData.Maybe.Just _
if any of the values exist orData.Maybe.Nothing
if none of them do. @gabejohnson mentioned that it'd be akin toOption.getAll
, but only returnData.Maybe.Nothing
if no values are there (as opposed to at least one value not being there). @gabejohnson also suggestedOption.getSome
as the name of the value.The problem
While it seemed feasible at first, I think it's a value that would end up being hard to use. An example might help. Let's say we have:
What we want is a family of functions:
We can probably write a typeclass and instance(s) for this family of functions. The hard part is using it. If we were to say:
What would we expect
?option
to be? There's four valid choices for it, and if we choose the wrong one, we'll might get aData.Maybe.Nothing
when we didn't expect it . That's not really what we wanted. We wanted to take anyOption.Option _
, and only get aData.Maybe.Nothing
if none of the values were there.The real implementation?
It almost seems like what we want is something like:
This way, we'll at least have a single type that is always the same. You'd be able to discriminate the cases dynamically instead of having to take a guess statically.
An alternative implementation
Instead of creating that family of functions, we can throw more
Data.Maybe.Maybe _
s in the mix and have a single function:I think this is actually more inline with the specific example @gabejohnson was dealing with. That seems like something we could throw together immediately as:
We can open up the
Option.Option _
and check if the underlyingForeign.Object.Object _
is empty. If it is, there's no values, so we can returnData.Maybe.Nothing
. Otherwise, we grab what we can.My main qualm with this implementation is that anyone consuming it still has to handle the
Data.Maybe.Just { name: Data.Maybe.Nothing, title: Data.Maybe.Nothing }
case. That doesn't seem like it should be a possible value, but the types still allow it.The current workaround
Assuming the
Option.Option _
can have aData.Eq.Eq _
instance, there's a way to get the alternative implementation without adding something to theOption
module. Anyone can write the following value external to theOption
module:If the
Option.Option _
has values without aData.Eq.Eq _
instance, they can't use that value. It's not an end-all, but it should unstick while we try to figure things out here.What to do?
I'd like to sit on this for a while. Each of the implementations above come with their own set of issues: hard to choose a type, illegal states represented, additional constraints.
The text was updated successfully, but these errors were encountered: