Skip to content

Latest commit

 

History

History
110 lines (76 loc) · 2.94 KB

FS-1007-additional-Option-module-functions.md

File metadata and controls

110 lines (76 loc) · 2.94 KB

F# RFC FS-1007 - Additional Option module functions

The design suggestion Additional Option module functions has been marked "approved in principle".
This RFC covers the detailed proposal for this suggestion.

Discussion thread

Introduction

Additional functions to Option module

Approved functions

Functions that provide default values in case of None

The approved names and forms are:

val defaultValue :         'a         -> 'a option -> 'a
val defaultWith : (unit -> 'a)        -> 'a option -> 'a
val orElse :               'a option  -> 'a option -> 'a option
val orElseWith :  (unit -> 'a option) -> 'a option -> 'a option

Option.map overloads

val map2: ('a -> 'b -> 'c) -> 'a option -> 'b option -> 'c option
val map3: ('a -> 'b -> 'c -> 'd) -> 'a option -> 'b option -> 'c option -> 'd option

Option.contains

val contains: 'a -> 'a option -> bool

Option.flatten

val flatten: 'a option option -> 'a option

Denied functions

Option.apply

val apply: 'a option -> ('a -> 'b) option -> 'b option

We don't have any other functions in the F# core library working over containers of function values. Uses of this function are very rare and using it doesn't particularly make code more readable or even much more succinct. Any programmer who can use it correctly can write the one line helper.

Functions not acted on

These functions may be brought up at a later time. Action was not taken on these function definitions:

Handling the result of TryParse-style functions

val ofTry : bool * 'a -> 'a option

Proposed names:

  • ofTry
  • ofByRef

Use case:

let parseInt x =
  match System.Int32.TryParse x with
  | false, _ -> None
  | true, v -> Some v

becomes

let parseInt x =
  System.Int32.TryParse x
  |> Option.ofByRef

Add toSeq

Provides a more complete relation to the ArrayListSeq set, as toArray and toList already exist.

val toSeq : 'a option -> 'a seq

Under discussion:

  • inline modifier: Should functions in the Option module be marked as inline? If so, which ones would benefit the most?
  • study adopting ExtCore.Option module

Performance considerations

The standard range of performance considerations for F# library functions apply.

Testing considerations

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