Writing F# Type Providers with the F# 3.0 Developer Preview - An Introductory Guide and Samples

A significant part of F# 3.0 support for F# Information Rich Programming is the F# Type Provider mechanism. An F# type provider is a component that provides types, properties, and methods for use in your program. For more information on F# 3.0 and the F# Type Provider mechanism, see What’s New for Visual F# in Visual Studio 11 Developer Preview.

We're very pleased to announce the availability of an introductory guide to writing F# 3.0 type providers, along with samples! The MSDN pages are here ( http://msdn.microsoft.com/en-us/library/hh361034(v=vs.110).aspx) . The samples and support code are part of the F# 3.0 Samples Pack on codeplex.The samples include 

  • The HelloWorldTypeProvider which shows how to some simple provide types, properties and methods
  • The RegexTypeProvider which shows how to give typesafe and statically checked access to regular expressions
  • The MiniCsvTypeProvider which gives typesafe access to CSV files based on a schema given on the first line of the file.

To use this guide you will need to install the Visual Studio 11 Developer Beta. This is preliminary documentation and should not be considered a definitive or final guide to writing type providers.

To give you a taste, the code below shows writing a provider which provides one type with one static member.

open Samples.FSharpPreviewRelease2011.ProvidedTypes

open Microsoft.FSharp.Core.CompilerServices


type SampleTypeProvider(config: TypeProviderConfig) as this =

   inherit TypeProviderForNamespaces()

   let namespaceName = "Samples.HelloWorldTypeProvider"

   let thisAssembly = Assembly.GetExecutingAssembly()

   let theType =


                              typeName = "Type" + string n,

                              baseType = Some typeof<obj>,

                              IsErased = true)

   let staticProp =

       ProvidedProperty(propertyName = "StaticProperty",

                        propertyType = typeof<string>,


                        GetterCode= (fun args -> <@@ "Hello!" @@>))

   do theType.AddMember staticProp

   do this.AddNamespace(namespaceName, [ theType ]



Please send us your feedback! You may submit feedback by emailing fsbugs@microsoft.com. Please note that this document is not designed to be a specification of the F# 3.0 type provider mechanism, which we will be publishing separately (though not as part of the Developer Preview). Some information is available on the MSDN pages.

Don Syme, Keith Battocchi and the Visual Studio F# Team