Home
Insert
Update snippet 'Using Active patterns'
Title
Description
Shows how to use an active pattern that will try to determine the format of an input value.
Source code
open System // definition of the active pattern let (|Bool|Int|Float|String|) input = // attempt to parse a bool let sucess, res = Boolean.TryParse input if sucess then Bool(res) else // attempt to parse an int let sucess, res = Int32.TryParse input if sucess then Int(res) else // attempt to parse a float (Double) let sucess, res = Double.TryParse input if sucess then Float(res) else String(input) // function to print the results by pattern // matching over the active pattern let printInputWithType input = match input with | Bool b -> printfn "Boolean: %b" b | Int i -> printfn "Integer: %i" i | Float f -> printfn "Floating point: %f" f | String s -> printfn "String: %s" s // print the results printInputWithType "true" printInputWithType "12" printInputWithType "-12.1"
Tags
active pattern
active pattern
Author
Link
Reference NuGet packages
If your snippet has external dependencies, enter the names of NuGet packages to reference, separated by a comma (
#r
directives are not required).
Update