F# get object data type

This function prints the data type of an object to the standard output.

let getObjectType (x:obj) =
    match x with
    | :? sbyte       as y -> printfn  "sbyte : %A" y
    | :? byte        as y -> printfn  "byte : %A" y
    | :? int16       as y -> printfn  "int16 : %A" y
    | :? uint16      as y -> printfn  "uint16 : %A" y
    | :? int32       as y -> printfn  "int32 : %A (int)" y
//    | :? int         as y -> printfn  "int : %A" y            
//    //   int is actually the same as int32
    | :? uint32      as y -> printfn  "uint32 : %A" y
    | :? int64       as y -> printfn  "int64 : %A" y
    | :? uint64      as y -> printfn  "uint64 : %A" y
    | :? bigint      as y -> printfn  "bigint : %A" y
    | :? float32     as y -> printfn  "float32 : %A" y
    | :? float       as y -> printfn  "float : %A" y
    | :? decimal     as y -> printfn  "decimal : %A" y
//    | :? BigRational as y -> printfn  "BigRational : %A" y    
//    //   BigRational requires a reference to FSharp.PowerPack.dll
    | :? char        as y -> printfn  "char : %A" y
    | :? string      as y -> printfn  "string : %A" y
    | :? bool        as y -> printfn  "bool : %A" y
    | :? System.DateTime as y -> printfn "DateTime : %A" y
    | _ -> printfn "unknown data type"

List of F# data types: https://www.tutorialspoint.com/fsharp/fsharp_data_types.htm

Leave a Reply

Your email address will not be published. Required fields are marked *