f# - dynamically determine type of Option when it has value 'None' -


There is some difficulty in getting the option type dynamically I suppose I have a function:

  print type x = if (box x) = then printfn "type is' blank '' else printfn" type% a "(x.GetType ())  

and our Here's the output:

  printType 3 // is here System.Int32 printType (some (3)) // type Microsoft.FSharp.Core.FSharpOption`1 [System.Int32] printType None // Type tap print type is tap / / the type is empty  

how to differentiate between someone else When getting an expression type zero?

edit: Code quotes are not required and types of types can be used instead, see the answer of @Tomas Petricek

You use a code quote to type in any way Can

  open Microsoft. FSHRP.X = Type OFX = & lt; @ X @ & gt; Type if not & lt; | FSharpType.IsUnion (typeOfX) & amp; Amp; (Box x) = null then printfn "type is 'zero'" and printfn "is type% A" typeOfX  

Your input:

  PrintType 3 // Type is System.Int32 printType (some (3)) / / type Microsoft. FSHRP.core FSARPOption '1 [System.INT 32] Print Type None / This is Microsoft. FSHR.Core.phasePoption `1 [System.Object] is printType null // type 'null'  

Comments