Serras explains typeof(MSIL)

I recently posted a note to the F# list on how to get the effect of typeof(...) in F# code through the use of F#'s inline Common IL.  The code I used has now been beautifully explained by Alejandro Serrano (who assures me he is a high-school student from Spain!). 

Furthermore, the shortly-to-be-announced F# 1.0.1 incoroprates the typeof operator into the Microsoft.FSharp.Idioms module, a typical use being:

  (typeof() : typ<CustomVertex.PositionColored>).result

Now, back to putting those finishing touches on F# 1.0.1....

[ Technical note: the implementation is

let inline typeof () : typ<'a> =
    let tok = (# "ldtoken !0" type('a) : System.RuntimeTypeHandle ) in
    let res = System.Type.GetTypeFromHandle(tok) in
    { result = res }

The "inline" is required to ensure the code works with or without generics: without generics, F# use template expansion to implement these sorts of type-specific helper functions.  The generic functions in CompatArray for manipulating array types work the same way.  ]


Comments (1)

  1. Thanks for linking to my blog. Apart from articles I’m going to publish a guide to F# for beginner. Check it!

Skip to main content