Friday, October 7, 2011

Authoring Type Providers with the TypeProviderDSL from FSharpx

Several days ago, I submitted my AppSettings Type Provider to the FSharpx project. FSharpx is an open source library that adds a number of useful "functional constructs on top of the core F# library". I strongly encourage checking it out.

One of the many things in FSharpx that I find useful is the TypeProviderDSL. This is a DSL that sits on top of the ProvidedTypes module provided in the F# 3.0 Sample Pack. With this DSL and a few other helpers in FSharpx, the code from the AppSettings Type Provider that I showed in a previous post becomes easier to read. Plus, a few lines of code are shaved off in the process.

Let's look at a few of the features of the TypeProviderDSL that are used by the AppSettings Type Provider.

1. The erasedType function provides a simple way to create a ProvidedTypeDefinition.
2. The staticParameter function encapsulates the code needed to define a static parameter.
3. The literalField function makes it slightly easier to create a ProvidedLiteralField.
4. The addXmlDoc function provides a common way to add XML documentation.
5. Lastly, the |+> operator makes it very easy to add members.

Here's the end result:



You can get FSharpx.TypeProviders from the NuGet Gallery at http://www.nuget.org/List/Packages/FSharpx.TypeProviders and find the source on GitHub at https://github.com/fsharp/fsharpx/tree/TypeProviders.

1 comment: