tryParse

Namespace: FsToolkit.ErrorHandling

tryParse tries to parse a string value into a specified type using the type's TryParse method. Returns Some value if parsing succeeds, or None if it fails.

Note: This function is not available when using Fable (JavaScript/Python compilation).

Function Signature

string -> ^value option

where ^value has a static member TryParse: string * byref<^value> -> bool.

Examples

Example 1 — Parsing an integer

let result : int option =
    Option.tryParse "42"

// Some 42

Example 2 — Parsing failure

let result : int option =
    Option.tryParse "not a number"

// None

Example 3 — Parsing a boolean

Example 4 — Parsing a DateTime

Last updated