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

Task wrapped result + option helpers #184

Closed
sheridanchris opened this issue Oct 18, 2022 · 1 comment · Fixed by #186
Closed

Task wrapped result + option helpers #184

sheridanchris opened this issue Oct 18, 2022 · 1 comment · Fixed by #186
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@sheridanchris
Copy link
Contributor

sheridanchris commented Oct 18, 2022

Is your feature request related to a problem? Please describe.
Helper functions to work with Task<Result<'success option, 'error>>

Example:

type Error =
  | UsernameTaken
  | DatabaseError of DbError

taskResult {
  do!
    // getUserByUsername : string -> Task<Result<User option, DbError>>
    getUserByUsername createAccountRequest.Username // 
    |> TaskResult.mapError DatabaseError
    |> TaskResult.bindRequireNone UsernameTaken
}

Describe the solution you'd like
Helper functions that combine bind + requireSome, requireNone, etc...

// name needs work :P
let inline bindRequireSome (error: 'error) (taskResultOption: Task<Result<'success option, 'error>>) =
    taskResultOption
    |> bind (Result.requireSome error >> Task.singleton)

Describe alternatives you've considered
N/A

Additional context
N/A

I would like to create a PR for this.

@TheAngryByrd TheAngryByrd added enhancement New feature or request good first issue Good for newcomers labels Oct 18, 2022
@TheAngryByrd
Copy link
Collaborator

Yeah it seems like the only work around is currently to do it in two binds

let getUserName () = taskResult {
    return Some "jimmy"
}

let main () = taskResult {
    let! userNameOpt = getUserName ()
    let! userName =  Result.requireSome "No username" userNameOpt 
    return ()
}

Seems like a good addition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants