FsToolkit.ErrorHandling icon indicating copy to clipboard operation
FsToolkit.ErrorHandling copied to clipboard

helper to convert option to result with transform

Open joprice opened this issue 10 months ago • 1 comments

I may have overlooked a helper in either this or the standard library, but I end up finding this function useful, similar to requireSome but where the fallback is evaluated on demand.

let fromOption f = (unit -> 'a) -> option<'b> -> Result<'b,'a> 
      function
      | Some value -> Ok value
      | None -> Error(f ())

Is there an equivalent or would it make sense to add perhaps Result.requireSomeWith?

joprice avatar Jan 08 '25 14:01 joprice

Agreed! Seem like a good PR to have:

  • Add requireSomeWith and requireNoneWith
  • Add to Result, AsyncResult, TaskResult, JobResult
  • Add corresponding tests

TheAngryByrd avatar Jan 08 '25 15:01 TheAngryByrd