FSharpPlus


Parsing

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

Parse

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

or

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

TryParse

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>
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 array -> unit + 4 overloads member Equals: comparand: obj -> bool member GetAddressBytes: unit -> byte array 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 array) : Net.IPAddress
Net.IPAddress(newAddress: int64) : Net.IPAddress
Net.IPAddress(address: ReadOnlySpan<byte>) : Net.IPAddress
Net.IPAddress(address: byte array, 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)

--------------------
type int = int32

--------------------
type int<'Measure> = int