This document covers the detailed proposal for this suggestion.
- Implementation (with discussion): In progress
- Decision: not going ahead with this
Ability to convert from a non-null string to an string option
, taking into account empty strings and whitespace-only strings.
Given the Option.ofObject
combinator introduced in F#4.x, it is a natural extension to want to convert from a String
to a
string option
in many cases. Unfortunately an empty or whitespace-only string will be mapped as to some string rather than
None
- this can lead, particularly for beginners, to bugs or unexpected behaviour at runtime, as well as boilerplate code
that performs this check explicitly.
This would be a simple function added to the existing Option
module in FSharp.Core which would take in a string and return an option.
Example code:
let ofString (value:System.String) =
if System.String.IsNullOrWhiteSpace value then None else Some value
let empty = Option.ofString "" // None
let whitespace = Option.ofString " " // None
let someContent = Option.ofString " test " // Some " test "
Potential for confusion for a beginner - when to use Option.ofObj
and Option.ofString
?
Increase of surface area?
Manual implementation across all projects that need this. It's a one line function so easy to do, but it crops up very often, hence the desire to incorporate into FSharp.Core
- What name should this function have? Possibly
Option.ofString
might imply that this function parses the contents of string somehow, although the type signature of string -> string option (plus xml comments) should make this clear.