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
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:int * ?format:ReadOnlySpan<char> * ?provider:IFormatProvider -> bool
    static val MaxValue : uint16
    static val MinValue : uint16
    static member Parse : s:string -> uint16 + 4 overloads
    static member TryParse : s:string * result:uint16 -> bool + 3 overloads
Multiple items
val option : f:('g -> 'h) -> n:'h -> _arg1:'g option -> 'h

type 'T option = Option<'T>
val tryParse : value:string -> 'b option (requires member TryParse)
namespace System.Net
Multiple items
type IPAddress =
  new : newAddress:int64 -> IPAddress + 4 overloads
  member Address : int64 with get, set
  member AddressFamily : AddressFamily
  member Equals : comparand:obj -> bool
  member GetAddressBytes : unit -> byte[]
  member GetHashCode : unit -> int
  member IsIPv4MappedToIPv6 : bool
  member IsIPv6LinkLocal : bool
  member IsIPv6Multicast : bool
  member IsIPv6SiteLocal : bool

Net.IPAddress(newAddress: int64) : Net.IPAddress
Net.IPAddress(address: byte []) : 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)
val parsed : int option
Multiple items
val int : value:'T -> int (requires member op_Explicit)

type int = int32

type int<'Measure> = int