Welcome to MSDN Blogs Sign in | Join | Help

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.  ]

 

Published Tuesday, January 25, 2005 1:24 AM by dsyme
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

Friday, January 28, 2005 7:29 PM by Alejandro Serrano

# Thanks for linking

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

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker