5 people like it.

Optional TryParse

Wrap TryParse in an option.

1: 
2: 
3: 
4: 
5: 
6: 
let inline tryParse< ^T when ^T : (static member TryParse : string * byref< ^T > -> bool) and  ^T : (new : unit -> ^T) > valueToParse =
    let mutable output = new ^T()
    let parsed = ( ^T : (static member TryParse  : string * byref< ^T > -> bool ) (valueToParse, &output) )
    match parsed with
    | true -> output |> Some
    | _ -> None
val tryParse : valueToParse:string -> 'T option (requires member TryParse and default constructor)

Full name: Script.tryParse
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
type byref<'T> = (# "<Common IL Type Omitted>" #)

Full name: Microsoft.FSharp.Core.byref<_>
type bool = System.Boolean

Full name: Microsoft.FSharp.Core.bool
type unit = Unit

Full name: Microsoft.FSharp.Core.unit
val valueToParse : string
val mutable output : 'T (requires member TryParse and default constructor)
val parsed : bool
union case Option.Some: Value: 'T -> Option<'T>
union case Option.None: Option<'T>
Raw view Test code New version

More information

Link:http://fssnip.net/sr
Posted:8 years ago
Author:Craig Boucher
Tags: static , constraint , tryparse , option