Skip to content

Latest commit

 

History

History
71 lines (47 loc) · 2.1 KB

FS-1004-result-type.md

File metadata and controls

71 lines (47 loc) · 2.1 KB

F# RFC FS-1004 - Result type

The design suggestion Result type has been marked "approved in principle". This RFC covers the detailed proposal for this suggestion.

Discussion thread

Introduction

In order to be able to write code that you can easily consume that does not throw an exception it's useful to have a fsharp type who contains the result on success or the error on failure

type Result<'TSuccess,'TError> = 
     | Success of 'TSuccess 
     | Error of 'TError

The type should be added in FSharp.Core library

Naming

The name of the type and union cases has not been finalized. Some of the suggestions are

Type name Success case Failure case
Result Success Failure
Result Success Error
Result OK Error
Result OK Err
Result OK Bad
Result Ok Error
Result Ok Err
Result Ok Bad

Testing considerations

The standard range of testing considerations for F# library types apply.

Open Questions

  • namespace
  • declare as struct
  • compiled name
  • require additional functions?

Proposal for additional functions

Uncontentious:

bind : ('T -> Result<'U, 'TError>) -> Result<'T, 'TError> -> Result<'U, 'TError>
map : ('T -> 'U) -> Result<'T, 'TError> -> Result<'U, 'TError>
mapError : ('TError -> 'U) -> Result<'T, 'TError> -> Result<'T, 'U>

A result builder should be a separate RFC which covers adding builders for both Option and Result.

Suggested:

attempt : (unit -> 'a) -> Result<'a,exn>