The F# Type Provider Starter Pack is two things:
-
The
ProvidedTypes.fs
API files you need to author type providers -
Documentation and samples on type provider creation
This package is actively seeking contributions. We are aiming for documentation (the plan is a GitHub page setup similar to FSharp.Data) with examples of all the most common features of Type Providers that people will want to use (basic erased type generation, parameterized providers, full generated types, seperating design and runtime how's and why's, etc). It will also become the main place for improvements and additions to the ProvidedTypes code.
Building a type provider nearly always starts with adding these files to your project:
- ProvidedTypes.fsi
- ProvidedTypes.fs
The Starter Pack NuGet package contains both these files as well as a set of debugging helpers, and when you install it, it should add them all to your F# project. It's probably best not to modify the files after adding them as upgrades to the package will ask to replace the previous versions - either submit changes back to this project or shadow the relevant functions in a seperate file.
If using Paket, you can also add code files by direct GitHub references.
Type providers may be used in projects that generate portable code or target other .NET Frameworks than that being used by the F# compiler. To convert an erasing type provider to a cross-targeting erasing type provider, add the following source files to your project:
- AssemblyReader.fs
- AssemblyReaderReflection.fs
- ProvidedTypesContext.fs
Then add
let ctxt = ProvidedTypesContext.Create(config)
to your code and always create provided entities using this ctxt
object:
let myType = ctxt.ProvidedTypeDefinition(asm, ns, "MyType", Some typeof<obj>)
This is shown in the example below.
Here is a basic erasing type provider using the Provided Types API:
open ProviderImplementation
open ProviderImplementation.ProvidedTypes
open Microsoft.FSharp.Core.CompilerServices
open System.Reflection
[<TypeProvider>]
type BasicProvider (config : TypeProviderConfig) as this =
inherit TypeProviderForNamespaces ()
let ns = "StaticProperty.Provided"
let asm = Assembly.GetExecutingAssembly()
let ctxt = ProvidedTypesContext.Create(config)
let createTypes () =
let myType = ctxt.ProvidedTypeDefinition(asm, ns, "MyType", Some typeof<obj>)
let myProp = ctxt.ProvidedProperty("MyProperty", typeof<string>, IsStatic = true, getterCode = (fun args -> <@@ "Hello world" @@>))
myType.AddMember(myProp)
[myType]
do
this.AddNamespace(ns, createTypes())
[<assembly:TypeProviderAssembly>]
do ()
For advice on how to get started building a type provider, check out:
- Type Providers from the ground up
- (and the follow up posts)
- The MSDN Tutorial. The code in this package replaces the code from the sample pack it mentions.
- If you have a question about
FSharp
, ask at StackOverflow and mark your question with thef#
tag. - If you want to submit a bug, a feature request or help with fixing bugs then look at issues.
- To discuss more general issues about F# Type Providers Starter Pack, its goals and other open-source F# projects, join the fsharp-opensource mailing list
Use
build.sh RunTests
or
build.cmd RunTests
The library is available under Apache 2.0. For more information see the License file in the GitHub repository.
The default maintainer account for projects under "fsprojects" is @fsprojectsgit - F# Community Project Incubation Space (repo management)