F#+ provides several helper methods in order to simplify building parsers and parsing like tasks.


Parse allows you to use parse generic method for standard types and types that implement a static Parse method with the correct signature.

Minimal definition

static member Parse (x:'r) :'T


static member Parse (x:'r, c:CultureInfo) :'T


TryParse allows you to use tryParse generic method for standard types and types that implement a static TryParse method with the correct signature.

Minimal definition

In order to use tryParse together with a type the type needs to implement a TryParse like static method.

You can use F# style TryParse:

static member TryParse(value:'r) : 'T option

or C# style TryParse:

static member TryParse (x:'r, [<Out>] result: 'T byref) :bool

expressed in C# that would be:

public static bool TryParse (string x, out T result) 

A neat thing when you have types that implement the above definition is that it's simple to define active patterns:

let (|Port|_|) : _-> UInt16 option = tryParse
let (|IPAddress|_|) :_->System.Net.IPAddress option = tryParse

sscanf, trySscanf and friends

In F# you have some nice utility functions for creating printf style string writer function. In F#+ we find the inverse: sscanf and trySscanf.

For instance if you want to parse based on known format of a url:

let route1 x = trySscanf "/api/resource/%d" x
let parsed : int option = route1 "/api/resource/1"
namespace System
namespace FSharpPlus
[<Struct>] type UInt16 = member CompareTo: value: obj -> int + 1 overload member Equals: obj: obj -> bool + 1 overload member GetHashCode: unit -> int member GetTypeCode: unit -> TypeCode member ToString: unit -> string + 3 overloads member TryFormat: destination: Span<char> * charsWritten: byref<int> * ?format: ReadOnlySpan<char> * ?provider: IFormatProvider -> bool static member Parse: s: ReadOnlySpan<char> * ?style: NumberStyles * ?provider: IFormatProvider -> uint16 + 4 overloads static member TryParse: s: ReadOnlySpan<char> * style: NumberStyles * provider: IFormatProvider * result: byref<uint16> -> bool + 3 overloads static val MaxValue: uint16 static val MinValue: uint16
<summary>Represents a 16-bit unsigned integer.</summary>
Multiple items
val option: f: ('g -> 'h) -> n: 'h -> _arg1: 'g option -> 'h
<summary> Takes a function, a default value and a option value. If the option value is None, the function returns the default value. Otherwise, it applies the function to the value inside Some and returns the result. </summary>
<category index="0">Common Combinators</category>

type 'T option = Option<'T>
<summary>The type of optional values. When used from other CLI languages the empty option is the <c>null</c> value. </summary>
<remarks>Use the constructors <c>Some</c> and <c>None</c> to create values of this type. Use the values in the <c>Option</c> module to manipulate values of this type, or pattern match against the values directly. 'None' values will appear as the value <c>null</c> to other CLI languages. Instance methods on this type will appear as static methods to other CLI languages due to the use of <c>null</c> as a value representation.</remarks>
<category index="3">Options</category>
val tryParse: value: string -> 'T option (requires member TryParse)
<summary> Converts to a value from its string representation. Returns None if the convertion doesn't succeed. </summary>
<category index="21">Converter</category>
namespace System.Net
Multiple items
type IPAddress = new: address: byte[] -> unit + 4 overloads member Equals: comparand: obj -> bool member GetAddressBytes: unit -> byte[] member GetHashCode: unit -> int member MapToIPv4: unit -> IPAddress member MapToIPv6: unit -> IPAddress member ToString: unit -> string member TryFormat: destination: Span<char> * charsWritten: byref<int> -> bool member TryWriteBytes: destination: Span<byte> * bytesWritten: byref<int> -> bool static member HostToNetworkOrder: host: int16 -> int16 + 2 overloads ...
<summary>Provides an Internet Protocol (IP) address.</summary>

Net.IPAddress(address: byte[]) : Net.IPAddress
Net.IPAddress(newAddress: int64) : Net.IPAddress
Net.IPAddress(address: ReadOnlySpan<byte>) : Net.IPAddress
Net.IPAddress(address: byte[], scopeid: int64) : Net.IPAddress
Net.IPAddress(address: ReadOnlySpan<byte>, scopeid: int64) : Net.IPAddress
val route1: x: string -> int option
val x: string
val trySscanf: pf: PrintfFormat<'a,'b,'c,'d,'(T1 * T2 * ... * Tn)> -> s: string -> '(T1 * T2 * ... * Tn) option (requires member TryParseArray)
<summary> Gets a tuple with the result of parsing each element of a formatted text. Returns None in case of failure. </summary>
val parsed: int option
Multiple items
val int: value: 'T -> int (requires member op_Explicit)
<summary>Converts the argument to signed 32-bit integer. This is a direct conversion for all primitive numeric types. For strings, the input is converted using <c>Int32.Parse()</c> with InvariantCulture settings. Otherwise the operation requires an appropriate static conversion method on the input type.</summary>
<param name="value">The input value.</param>
<returns>The converted int</returns>
<example id="int-example"><code lang="fsharp"></code></example>

[<Struct>] type int = int32
<summary>An abbreviation for the CLI type <see cref="T:System.Int32" />.</summary>
<category>Basic Types</category>

type int<'Measure> = int
<summary>The type of 32-bit signed integer numbers, annotated with a unit of measure. The unit of measure is erased in compiled code and when values of this type are analyzed using reflection. The type is representationally equivalent to <see cref="T:System.Int32" />.</summary>
<category>Basic Types with Units of Measure</category>